Last active
August 29, 2015 14:26
-
-
Save emrahgunduz/5c27ff1db650b93c7864 to your computer and use it in GitHub Desktop.
Emscripten handleMessage -- String to Uint8Array convertion -- library_sockfs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function handleMessage(data) { | |
if( typeof data == 'string' ) { | |
var strUtf8 = unescape( encodeURIComponent( data ) ); | |
var ab = new Uint8Array( strUtf8.length ); | |
var le = strUtf8.length; | |
for ( var i = 0; i < le; i++ ) { | |
ab[i] = strUtf8.charCodeAt(i); | |
} | |
data = ab; | |
} else { | |
data = new Uint8Array(data); // make a typed array view on the array buffer | |
} | |
// console.log(data); | |
#if SOCKET_DEBUG | |
Module.print('websocket handle message (' + data.byteLength + ' bytes): ' + [Array.prototype.slice.call(data)]); | |
#endif | |
// if this is the port message, override the peer's port with it | |
var wasfirst = first; | |
first = false; | |
if (wasfirst && | |
data.length === 10 && | |
data[0] === 255 && data[1] === 255 && data[2] === 255 && data[3] === 255 && | |
data[4] === 'p'.charCodeAt(0) && data[5] === 'o'.charCodeAt(0) && data[6] === 'r'.charCodeAt(0) && data[7] === 't'.charCodeAt(0)) { | |
// update the peer's port and it's key in the peer map | |
var newport = ((data[8] << 8) | data[9]); | |
SOCKFS.websocket_sock_ops.removePeer(sock, peer); | |
peer.port = newport; | |
SOCKFS.websocket_sock_ops.addPeer(sock, peer); | |
return; | |
} | |
sock.recv_queue.push({ addr: peer.addr, port: peer.port, data: data }); | |
Module['websocket'].emit('message', sock.stream.fd); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment