|
| 1 | +/**********************************************************\ |
| 2 | +| | |
| 3 | +| hprose | |
| 4 | +| | |
| 5 | +| Official WebSite: http://www.hprose.com/ | |
| 6 | +| http://www.hprose.org/ | |
| 7 | +| | |
| 8 | +\**********************************************************/ |
| 9 | +/**********************************************************\ |
| 10 | + * * |
| 11 | + * WebSocketClient.js * |
| 12 | + * * |
| 13 | + * hprose websocket client for HTML5. * |
| 14 | + * * |
| 15 | + * LastModified: Apr 17, 2015 * |
| 16 | + * Author: Ma Bingyao <[email protected]> * |
| 17 | + * * |
| 18 | +\**********************************************************/ |
| 19 | + |
| 20 | +(function (global) { |
| 21 | + 'use strict'; |
| 22 | + |
| 23 | + var Exception = global.hprose.Exception; |
| 24 | + var Client = global.hprose.Client; |
| 25 | + var BytesIO = global.hprose.BytesIO; |
| 26 | + var Completer = global.hprose.Completer; |
| 27 | + |
| 28 | + function noop(){} |
| 29 | + var s_id = 0; |
| 30 | + var s_completers = []; |
| 31 | + var s_timeoutId = []; |
| 32 | + var s_messages = []; |
| 33 | + |
| 34 | + global.hprose.WebSocketClient = function WebSocketClient(uri, functions) { |
| 35 | + Client.call(this, uri, functions); |
| 36 | + var _timeout = 0; |
| 37 | + var self = this; |
| 38 | + var ready = false; |
| 39 | + var ws; |
| 40 | + function send_message(id, request) { |
| 41 | + var bytes = new BytesIO(); |
| 42 | + bytes.writeByte((id >> 24) & 0xff); |
| 43 | + bytes.writeByte((id >> 16) & 0xff); |
| 44 | + bytes.writeByte((id >> 8) & 0xff); |
| 45 | + bytes.writeByte(id & 0xff); |
| 46 | + if (request.constructor === String) { |
| 47 | + bytes.writeString(request); |
| 48 | + } |
| 49 | + else { |
| 50 | + bytes.write(request); |
| 51 | + } |
| 52 | + var message = bytes.bytes; |
| 53 | + if (ArrayBuffer.isView) { |
| 54 | + ws.send(message); |
| 55 | + } |
| 56 | + else if (message.buffer.slice) { |
| 57 | + ws.send(message.buffer.slice(0, message.length)); |
| 58 | + } |
| 59 | + else { |
| 60 | + ws.send(message.buffer); |
| 61 | + } |
| 62 | + } |
| 63 | + function onopen(e) { |
| 64 | + ready = true; |
| 65 | + if (s_messages.length > 0) { |
| 66 | + for (var id in s_messages) { |
| 67 | + send_message(id, s_messages[id]); |
| 68 | + } |
| 69 | + s_messages = []; |
| 70 | + } |
| 71 | + } |
| 72 | + function onmessage(e) { |
| 73 | + var bytes = new BytesIO(e.data); |
| 74 | + var id = bytes.readByte() << 24; |
| 75 | + id = id | bytes.readByte() << 16; |
| 76 | + id = id | bytes.readByte() << 8; |
| 77 | + id = id | bytes.readByte(); |
| 78 | + var timeoutId = s_timeoutId[id]; |
| 79 | + var completer = s_completers[id]; |
| 80 | + delete s_timeoutId[id]; |
| 81 | + delete s_completers[id]; |
| 82 | + if (timeoutId !== undefined) { |
| 83 | + global.clearTimeout(timeoutId); |
| 84 | + timeoutId = undefined; |
| 85 | + } |
| 86 | + completer.complete(bytes.read(bytes.length - 4)); |
| 87 | + } |
| 88 | + function onclose(e) { |
| 89 | + connect(); |
| 90 | + } |
| 91 | + function onerror(e) { |
| 92 | + self.onerror("WebSocket", new Exception(e.data)); |
| 93 | + } |
| 94 | + function connect() { |
| 95 | + ready = false; |
| 96 | + ws = new WebSocket(self.uri); |
| 97 | + ws.binaryType = "arraybuffer"; |
| 98 | + ws.onopen = onopen; |
| 99 | + ws.onmessage = onmessage; |
| 100 | + ws.onerror = onerror; |
| 101 | + ws.onclose = onclose; |
| 102 | + } |
| 103 | + function send(request) { |
| 104 | + var completer = new Completer(); |
| 105 | + var timeoutId = undefined; |
| 106 | + if (_timeout > 0) { |
| 107 | + timeoutId = global.setTimeout((function (id) { |
| 108 | + return function() { |
| 109 | + delete s_completers[id]; |
| 110 | + delete s_timeoutId[id]; |
| 111 | + delete s_messages[id]; |
| 112 | + ws.close(); |
| 113 | + completer.completeError(new Exception('timeout')); |
| 114 | + } |
| 115 | + })(s_id), _timeout); |
| 116 | + } |
| 117 | + s_completers[s_id] = completer; |
| 118 | + s_timeoutId[s_id] = timeoutId; |
| 119 | + if (ready) { |
| 120 | + send_message(s_id, request); |
| 121 | + } |
| 122 | + else { |
| 123 | + s_messages[s_id] = request; |
| 124 | + } |
| 125 | + if (s_id < 0x7fffffff) { |
| 126 | + ++s_id; |
| 127 | + } |
| 128 | + else { |
| 129 | + s_id = 0; |
| 130 | + } |
| 131 | + return completer.future; |
| 132 | + } |
| 133 | + function setTimeout(value) { |
| 134 | + if (typeof(value) === 'number') { |
| 135 | + _timeout = value | 0; |
| 136 | + } |
| 137 | + else { |
| 138 | + _timeout = 0; |
| 139 | + } |
| 140 | + } |
| 141 | + function getTimeout() { |
| 142 | + return _timeout; |
| 143 | + } |
| 144 | + Object.defineProperties(this, { |
| 145 | + timeout: { get: getTimeout, set: setTimeout }, |
| 146 | + __send__: { value: send } |
| 147 | + }); |
| 148 | + connect(); |
| 149 | + }; |
| 150 | + |
| 151 | + function create(uri, functions) { |
| 152 | + var parser = document.createElement('a'); |
| 153 | + parser.href = uri; |
| 154 | + if (parser.protocol === 'ws:' || |
| 155 | + parser.protocol === 'wss:') { |
| 156 | + return new global.hprose.WebSocketClient(uri, functions); |
| 157 | + } |
| 158 | + throw new Exception('This client desn\'t support ' + parser.protocol + ' scheme.'); |
| 159 | + } |
| 160 | + |
| 161 | + Object.defineProperty(global.hprose.WebSocketClient, 'create', { value: create }); |
| 162 | +})(this); |
0 commit comments