|
1 | | -;(function(root) { |
2 | | - function websocket(dop, node, options) { |
3 | | - var url = 'ws://localhost:4444/' + dop.name, |
4 | | - oldSocket |
5 | | - |
6 | | - if (typeof options.url == 'string') |
7 | | - url = options.url.replace('http', 'ws') |
8 | | - else if ( |
9 | | - typeof window != 'undefined' && |
10 | | - typeof window.location != 'undefined' && |
11 | | - /http/.test(window.location.href) |
12 | | - ) { |
13 | | - var domain_prefix = /(ss|ps)?:\/\/([^\/]+)\/?(.+)?/.exec( |
14 | | - window.location.href |
15 | | - ), |
16 | | - protocol = domain_prefix[1] ? 'wss' : 'ws' |
17 | | - url = |
18 | | - protocol + |
19 | | - '://' + |
20 | | - domain_prefix[2].toLocaleLowerCase() + |
21 | | - '/' + |
22 | | - dop.name |
23 | | - } |
24 | | - |
25 | | - // Variables |
26 | | - var api = options.transport.getApi(), |
27 | | - socket = new api(url), |
28 | | - tokenServer, |
29 | | - send_queue = [], |
30 | | - readyState |
31 | | - |
32 | | - // Helpers |
33 | | - function send(message) { |
34 | | - socket.readyState === OPEN |
35 | | - ? socket.send(message) |
36 | | - : send_queue.push(message) |
37 | | - } |
38 | | - function sendQueue() { |
39 | | - if (socket.readyState === OPEN) |
40 | | - while (send_queue.length > 0) socket.send(send_queue.shift()) |
41 | | - } |
42 | | - |
43 | | - // Socket events |
44 | | - function onopen() { |
45 | | - // Reconnect |
46 | | - if (readyState === CONNECTING) socket.send(tokenServer) |
47 | | - // Connect |
48 | | - else { |
49 | | - socket.send('') // Empty means we want to get connected |
50 | | - readyState = OPEN |
51 | | - } |
52 | | - dop.core.emitOpen(node, socket, options.transport) |
53 | | - } |
54 | | - function onmessage(message) { |
55 | | - // console.log( 'C<<: `'+message.data+'`' ); |
56 | | - // Reconnecting |
57 | | - if (readyState === CONNECTING && message.data === tokenServer) { |
58 | | - readyState = CONNECT |
59 | | - dop.core.setSocketToNode(node, socket) |
60 | | - dop.core.emitReconnect(node, oldSocket) |
61 | | - sendQueue() |
62 | | - } else if (readyState !== CONNECT) { |
63 | | - tokenServer = message.data |
64 | | - readyState = CONNECT |
65 | | - dop.core.setSocketToNode(node, socket) |
66 | | - send(tokenServer) |
67 | | - sendQueue() |
68 | | - dop.core.emitConnect(node) |
69 | | - } else dop.core.emitMessage(node, message.data, message) |
70 | | - } |
71 | | - function onclose() { |
72 | | - readyState = CLOSE |
73 | | - dop.core.emitClose(node, socket) |
74 | | - dop.core.emitDisconnect(node) |
75 | | - } |
76 | | - |
77 | | - // dop events |
78 | | - // function onconnect() { |
79 | | - // if (readyState === CONNECTING) { |
80 | | - // dop.core.emitDisconnect(node); |
81 | | - // dop.core.setSocketToNode(node, socket); |
82 | | - // } |
83 | | - // readyState = CONNECT; |
84 | | - // dop.core.emitConnect(node); |
85 | | - // sendQueue(); |
86 | | - // } |
87 | | - function ondisconnect() { |
88 | | - readyState = CLOSE |
89 | | - socket.close() |
90 | | - } |
91 | | - |
92 | | - function reconnect() { |
93 | | - if (readyState === CLOSE) { |
94 | | - oldSocket = socket |
95 | | - socket = new api(url) |
96 | | - readyState = CONNECTING |
97 | | - addListeners(socket, onopen, onmessage, onclose) |
98 | | - removeListeners(oldSocket, onopen, onmessage, onclose) |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - // Setting up |
103 | | - dop.core.setSocketToNode(node, socket) |
104 | | - readyState = CLOSE |
105 | | - node.reconnect = reconnect |
106 | | - // node.on(dop.cons.CONNECT, onconnect); |
107 | | - node.on(dop.cons.SEND, send) |
108 | | - node.on(dop.cons.DISCONNECT, ondisconnect) |
109 | | - addListeners(socket, onopen, onmessage, onclose) |
110 | | - |
111 | | - return socket |
112 | | - } |
113 | | - |
114 | | - function addListeners(socket, onopen, onmessage, onclose) { |
115 | | - socket.addEventListener('open', onopen) |
116 | | - socket.addEventListener('message', onmessage) |
117 | | - socket.addEventListener('close', onclose) |
118 | | - } |
119 | | - function removeListeners(socket, onopen, onmessage, onclose) { |
120 | | - socket.removeEventListener('open', onopen) |
121 | | - socket.removeEventListener('message', onmessage) |
122 | | - socket.removeEventListener('close', onclose) |
123 | | - } |
124 | | - |
125 | | - // UMD |
126 | | - if ( |
127 | | - typeof module == 'object' && |
128 | | - module.exports && |
129 | | - !( |
130 | | - typeof dop == 'object' && |
131 | | - typeof factory == 'function' && |
132 | | - dop.create === factory |
133 | | - ) // this is true if we are inside of dop.factory |
134 | | - ) |
135 | | - module.exports = websocket |
136 | | - else { |
137 | | - websocket.getApi = function() { |
138 | | - return window.WebSocket |
139 | | - } |
140 | | - typeof dop != 'undefined' |
141 | | - ? (dop.transports.connect.websocket = websocket) |
142 | | - : (root.dopTransportsConnectWebsocket = websocket) |
143 | | - } |
144 | | - |
145 | | - // Cons |
146 | | - var CLOSE = 0, |
147 | | - OPEN = 1, |
148 | | - CONNECTING = 2, |
149 | | - CONNECT = 3 |
150 | | -})(this) |
| 1 | +;(function(root) { |
| 2 | + function websocket(dop, options) { |
| 3 | + var url = 'ws://localhost:4444/' + dop.name |
| 4 | + if (typeof options.url == 'string') { |
| 5 | + url = options.url.replace('http', 'ws') |
| 6 | + } else if ( |
| 7 | + typeof window != 'undefined' && |
| 8 | + typeof window.location != 'undefined' && |
| 9 | + /http/.test(window.location.href) |
| 10 | + ) { |
| 11 | + var domain_prefix = /(ss|ps)?:\/\/([^\/]+)\/?(.+)?/.exec( |
| 12 | + window.location.href |
| 13 | + ), |
| 14 | + protocol = domain_prefix[1] ? 'wss' : 'ws' |
| 15 | + url = |
| 16 | + protocol + |
| 17 | + '://' + |
| 18 | + domain_prefix[2].toLocaleLowerCase() + |
| 19 | + '/' + |
| 20 | + dop.name |
| 21 | + } |
| 22 | + |
| 23 | + var transport = dop.createTransport() |
| 24 | + var WebSocket = options.transport.getApi() |
| 25 | + ;(function reconnect() { |
| 26 | + var socket = new WebSocket(url) |
| 27 | + function send(message) { |
| 28 | + if (socket.readyState === 1) { |
| 29 | + socket.send(message) |
| 30 | + return true |
| 31 | + } |
| 32 | + return false |
| 33 | + } |
| 34 | + function disconnect() { |
| 35 | + socket.close() |
| 36 | + } |
| 37 | + var node = transport.onCreate(socket, send, disconnect) |
| 38 | + socket.addEventListener('open', function() { |
| 39 | + transport.onConnect(node) |
| 40 | + }) |
| 41 | + socket.addEventListener('message', function(message) { |
| 42 | + transport.onMessage(node, message.data) |
| 43 | + }) |
| 44 | + socket.addEventListener('close', function() { |
| 45 | + transport.onDisconnect(node) |
| 46 | + }) |
| 47 | + socket.addEventListener('error', function(error) { |
| 48 | + transport.onError(node, error) |
| 49 | + }) |
| 50 | + })() |
| 51 | + |
| 52 | + return transport |
| 53 | + } |
| 54 | + |
| 55 | + websocket.getApi = function() { |
| 56 | + return window.WebSocket |
| 57 | + } |
| 58 | + |
| 59 | + // UMD |
| 60 | + if ( |
| 61 | + typeof module == 'object' && |
| 62 | + module.exports && |
| 63 | + !( |
| 64 | + typeof dop == 'object' && |
| 65 | + typeof factory == 'function' && |
| 66 | + dop.create === factory |
| 67 | + ) // this is true if we are inside of dop.factory |
| 68 | + ) { |
| 69 | + module.exports = websocket |
| 70 | + } else { |
| 71 | + websocket.getApi = function() { |
| 72 | + return window.WebSocket |
| 73 | + } |
| 74 | + typeof dop != 'undefined' |
| 75 | + ? (dop.transports.connect.websocket = websocket) |
| 76 | + : (root.dopTransportsConnectWebsocket = websocket) |
| 77 | + } |
| 78 | +})(this) |
0 commit comments