WebSocketã§ãã¤ããªãéåä¿¡ãã¦ã¿ã(2)
ã¯ã©ã¤ã¢ã³ããããã¤ããªãéã確èªãã§ããã®ã§ã
ãã¤ããªã«æ´ã«ãã¼ã¿ä¹ã£ãã¦éã£ããããã£ãããã¦ã¿ãã
ã¯ã©ã¤ã¢ã³ãå´
input:fileã§ãã¡ã¤ã«ãåç
§ããããå
é ã«helloworldæååã追å ãã
ãã¤ããªãã¼ã¿ã¨ãã¦éä¿¡ããã
ãµã¼ãå´ããã¡ãã»ã¼ã¸ãåãåã£ã¦ããã¤ããªãã¼ã¿ã ã£ãã
å
é ã®æååã¨ç»åãã¼ã¿ã«åãã¦ãããã表示ããã
// BlobBuilder BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder; // URL URL = window.URL || window.webkitURL; // document var doc = document; // WebSocketãéã var socket = new WebSocket('ws://' + location.host); // ãã¤ããªãã¼ã¿ã¯ArrayBufferã§åå¾ãã socket.binaryType = 'arraybuffer'; // messageã¤ãã³ã socket.addEventListener('message', function(evt) { var data = evt.data; if (data.constructor === String) { // Stringã®å ´åãpã¿ã°ã§è¿½å var p = doc.createElement('p'); p.textContent = data; doc.getElementById('messages').appendChild(p); } else if (data.constructor === ArrayBuffer) { // ArrayBufferã®å ´åãä»å ãããæååã¨ç»åãã¼ã¿ã«åãã // å é ãã10ãã¤ãã8ããã符å·ãªãæ´æ°å¤ã®é åã« var arr = new Uint8Array(data.slice(0, 10)); var key = []; for (var i = 0; i < 10; i++) { // ãã£ã©ã¯ã¿ã³ã¼ãã§æååã«å¤æãã¦æ ¼ç´ key.push(String.fromCharCode(arr[i])); } // ä»å ããæååãpã¿ã°ã§è¿½å var p = doc.createElement('p'); p.textContent = key.join(''); doc.getElementById('messages').appendChild(p); // ç»åãã¼ã¿åãimgã¿ã°ã§è¿½å var builder = new BlobBuilder(); var img = new Image(); builder.append(data.slice(10)); img.src = URL.createObjectURL(builder.getBlob()); doc.getElementById('images').appendChild(img); } else { alert('nanigashi'); } }, false); // æååãã¼ã¿ããµã¼ãã«éä¿¡ doc.getElementById('sendText') .addEventListener('click', function(evt) { socket.send('test'); }, false); // fileãåç §ããããµã¼ãã«éä¿¡ doc.getElementById('image') .addEventListener('change', function(evt) { var file = evt.target.files[0]; var builder = new BlobBuilder(); builder.append('helloworld'); builder.append(file); // socket.send(file); socket.send(builder.getBlob()); }, false);
ãµã¼ãå´
ã¯ã©ã¤ã¢ã³ããããã¤ããªãã¼ã¿ãåãåã£ããæååã¨ç»åãã¼ã¿ã«å解ãã¦ã
æååãè¿ãããã¨ã«æ°ããªæååãå
é ã«ãã£ã¤ããç»åãã¼ã¿ãéä¿¡ããã
// http server var connect = require('connect'); var httpServer = connect() .use(connect.static(__dirname + '/webroot')) .listen(1234); // WebSocket Server var WebSocketServer = require('websocket').server; var wsServer = new WebSocketServer({ httpServer : httpServer, autoAcceptConnections : true }); // ã¯ã©ã¤ã¢ã³ãæ¥ç¶ã¤ãã³ã wsServer.on('connect', function(client) { // ã¯ã©ã¤ã¢ã³ãããã®ã¡ãã»ã¼ã¸åä¿¡ã¤ãã³ã client.on('message', function(message) { if (message.type === 'utf8') { // æååã ã£ããæååã¨ãã¦ãã®ã¾ã¾éä¿¡ client.sendUTF(message.utf8Data); } else if (message.type === 'binary') { // ãã¤ããªã ã£ããä»å ããã¦ããæååãåãåºã var src = message.binaryData; var text = new Buffer(10); src.copy(text, 0, 0, 10); // åãåã£ãæååãè¿ã client.sendUTF('receive:' + text.toString('utf8')); // æ°ããªæååãä»å ãã¦æ¸ãæãããã¤ããªãã¼ã¿ã¨ãã¦éä¿¡ var str = 'helloagain'; var key = new Buffer(str); var buf = new Buffer(src.length); key.copy(buf, 0); src.copy(buf, 10, 10); client.sendBytes(buf); } else { console.log('nanigashi'); } }); });
çµæ
ArrayBufferã使ã£ã¦ãããããããã¨ã§ããã¤ããªãã¼ã¿ãåå²ããããã¦
ããããã§ãããã¡ããã©Firefoxã12ããArrayBufferã®sliceãã§ããããã«ãªã£ã¦ãã