1 | 'use strict'
|
2 |
|
3 |
|
4 | var socketOpen = false
|
5 | var socketMsgQueue = []
|
6 |
|
7 | function sendSocketMessage (msg) {
|
8 | if (socketOpen) {
|
9 | wx.sendSocketMessage({
|
10 | data: msg
|
11 | })
|
12 | } else {
|
13 | socketMsgQueue.push(msg)
|
14 | }
|
15 | }
|
16 |
|
17 | function WebSocket (url, protocols) {
|
18 | var ws = {
|
19 | OPEN: 1,
|
20 | CLOSING: 2,
|
21 | CLOSED: 3,
|
22 | readyState: socketOpen ? 1 : 0,
|
23 | send: sendSocketMessage,
|
24 | close: wx.closeSocket,
|
25 | onopen: null,
|
26 | onmessage: null,
|
27 | onclose: null,
|
28 | onerror: null
|
29 | }
|
30 |
|
31 | wx.connectSocket({
|
32 | url: url,
|
33 | protocols: protocols
|
34 | })
|
35 | wx.onSocketOpen(function (res) {
|
36 | ws.readyState = ws.OPEN
|
37 | socketOpen = true
|
38 | for (var i = 0; i < socketMsgQueue.length; i++) {
|
39 | sendSocketMessage(socketMsgQueue[i])
|
40 | }
|
41 | socketMsgQueue = []
|
42 |
|
43 | ws.onopen && ws.onopen.apply(ws, arguments)
|
44 | })
|
45 | wx.onSocketMessage(function (res) {
|
46 | ws.onmessage && ws.onmessage.apply(ws, arguments)
|
47 | })
|
48 | wx.onSocketClose(function () {
|
49 | ws.onclose && ws.onclose.apply(ws, arguments)
|
50 | ws.readyState = ws.CLOSED
|
51 | socketOpen = false
|
52 | })
|
53 | wx.onSocketError(function () {
|
54 | ws.onerror && ws.onerror.apply(ws, arguments)
|
55 | ws.readyState = ws.CLOSED
|
56 | socketOpen = false
|
57 | })
|
58 |
|
59 | return ws
|
60 | }
|
61 |
|
62 | var websocket = require('websocket-stream')
|
63 | var urlModule = require('url')
|
64 |
|
65 | function buildUrl (opts, client) {
|
66 | var protocol = opts.protocol === 'wxs' ? 'wss' : 'ws'
|
67 | var url = protocol + '://' + opts.hostname + ':' + opts.port + opts.path
|
68 | if (typeof (opts.transformWsUrl) === 'function') {
|
69 | url = opts.transformWsUrl(url, opts, client)
|
70 | }
|
71 | return url
|
72 | }
|
73 |
|
74 | function setDefaultOpts (opts) {
|
75 | if (!opts.hostname) {
|
76 | opts.hostname = 'localhost'
|
77 | }
|
78 | if (!opts.port) {
|
79 | if (opts.protocol === 'wss') {
|
80 | opts.port = 443
|
81 | } else {
|
82 | opts.port = 80
|
83 | }
|
84 | }
|
85 | if (!opts.path) {
|
86 | opts.path = '/'
|
87 | }
|
88 |
|
89 | if (!opts.wsOptions) {
|
90 | opts.wsOptions = {}
|
91 | }
|
92 | }
|
93 |
|
94 | function createWebSocket (client, opts) {
|
95 | var websocketSubProtocol =
|
96 | (opts.protocolId === 'MQIsdp') && (opts.protocolVersion === 3)
|
97 | ? 'mqttv3.1'
|
98 | : 'mqtt'
|
99 |
|
100 | setDefaultOpts(opts)
|
101 | var url = buildUrl(opts, client)
|
102 | return websocket(WebSocket(url, [websocketSubProtocol]))
|
103 | }
|
104 |
|
105 | function buildBuilder (client, opts) {
|
106 | if (!opts.hostname) {
|
107 | opts.hostname = opts.host
|
108 | }
|
109 |
|
110 | if (!opts.hostname) {
|
111 |
|
112 |
|
113 |
|
114 | if (typeof (document) === 'undefined') {
|
115 | throw new Error('Could not determine host. Specify host manually.')
|
116 | }
|
117 | var parsed = urlModule.parse(document.URL)
|
118 | opts.hostname = parsed.hostname
|
119 |
|
120 | if (!opts.port) {
|
121 | opts.port = parsed.port
|
122 | }
|
123 | }
|
124 | return createWebSocket(client, opts)
|
125 | }
|
126 |
|
127 | module.exports = buildBuilder
|