diff options
author | Ray Strode <rstrode@redhat.com> | 2013-09-02 20:32:20 -0400 |
---|---|---|
committer | Ray Strode <rstrode@redhat.com> | 2014-01-15 10:21:27 -0500 |
commit | 7b198402c367098629ca4b831acaf47e18f330a5 (patch) | |
tree | bccba71a0946e2f2bbbb55a7feaffdb1b2fa58ad | |
parent | 94b9a615903710c2903b8830957103cee74bed39 (diff) |
This is just a start at getting webSocket I/O going
-rw-r--r-- | protocols/webSocket.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/protocols/webSocket.js b/protocols/webSocket.js index a9e7d14..c3d44ae 100644 --- a/protocols/webSocket.js +++ b/protocols/webSocket.js @@ -1,4 +1,5 @@ // Protocol: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-13 +const ByteArray = imports.byteArray; const GLib = imports.gi.GLib; const Gio = imports.gi.Gio; const Lang = imports.lang; @@ -95,11 +96,63 @@ const WebSocket = new Lang.Class({ // websocket stream, not an http stream this._session.pause_message(this._message); this._socket = new Gio.Socket({ fd: this._socketFd }); + this._socket.init(null, null); + let source = GLib.unix_fd_source_new(this._socketFd, GLib.IOCondition.IN); + source.set_callback(Lang.bind(this, this._onSocketReadable), null); + source.attach(null); + this._inputStream = new Gio.UnixInputStream({ fd: this._socketFd }); + this._outputStream = new Gio.UnixOutputStream({ fd: this._socketFd }); onOpened(); })); this._session.queue_message(this._message, null); + }, + + _onSocketReadable: function() { + let condition = this._socket.condition_check(GLib.IOCondition.IN); + + if (condition & GLib.IOCondition.IN) { + + let bytes = this._inputStream.read_bytes(4096, null, null); + + if (bytes.get_size() > 0) { + let array = new Array(bytes.get_size() - 2); + + let data = bytes.get_data(); + + print('received message'); + let controlBlock = data[0]; + print('control: ', controlBlock.toString(2)); + + let length = data[1] & ~(1 << 8); + print('length: ', length); + + let masked = (data[1] & (1 << 8)) != 0; + print('masked', masked); + + // I can't find a more efficient way to do this, despite + // GBytes, GByteArray, and imports.byteArray + // TODO; bytes = new GLib.Bytes(...); array = new Uint8Array(bytes.get_data(), bytes.get_size()) + for (let i = 0; i < array.length; i++) + array[i] = data[i + 2]; + + let string = ""; + if (controlBlock & 1) { + for (let i = 0; i < array.length; i++) { + string += String.fromCharCode(array[i]); + } + } else { + for (let i = 0; i < array.length; i++) { + string += array[i].toString(2) + ' '; + } + } + print(string); + print('end of message'); + } + } + + return true; } }); Signals.addSignalMethods(WebSocket.prototype); |