1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| const BASEURL = 'http://localhost:8070/' const SOCKETURL = 'ws://localhost:8070/socket'
App({ _websocket:{ client: null, subscriptions: [], messagesQueue: [], subscriptionQueue: [], clientAviable: false },
sendStomp: function(dest, msg, header){ if (this._websocket.clientAviable){ var res = this._websocket.client.send(dest, header, JSON.stringify(msg)) }else{ this._websocket.messagesQueue.push({ destination: dest, header: header, body: JSON.stringify(msg) }) } },
subscribeStomp: function (dest, callback, header) { this._websocket.subscriptions.push({ destination: dest, header: header, callback: callback }) if (this._websocket.clientAviable) { this._websocket.client.subscribe(dest, callback, header) } else { this._websocket.subscriptionQueue.push({ destination: dest, header: header, callback: callback }) } }, _sendSocketMessage: function(msg) { console.log('send msg:', msg); wx.sendSocketMessage({ data: msg }) },
connectWebsocket: function(param){ if (this.globalData.token) { this._connectWebsocketWithToken(param) } else { let that = this this.login({ tokenGot: function () { that._connectWebsocketWithToken(param) } }) } }, _closeSocket: function(pram){ wx.closeSocket({ success: function(res){}, fail: function(res){} }) }, _connectSocket: function(){ let that = this wx.connectSocket({ url: SOCKETURL, header: { "Authorization": "Bearer " + that.globalData.token }, success: function(res){
}, fail:function(res){ } }) }, _processQueue: function(client){ for (var i = 0; i < this._websocket.subscriptionQueue.length; i++) { var subs = this._websocket.subscriptionQueue[i] client.subscribe(subs.destination, subs.callback, subs.header) } this._websocket.subscriptionQueue = [] for (var i = 0; i < this._websocket.messagesQueue.length; i++) { var msg = this._websocket.messagesQueue[i] client.send(msg.destination, msg.header, msg.body) } this._websocket.messagesQueue = [] },
_connectWebsocketWithToken: function(param){ let that = this var ws = { send: this._sendSocketMessage, close: this._closeSocket, onopen: null, onmessage: null }
this._connectSocket()
wx.onSocketOpen(function (res) { console.log('websocket连接成功')
if (that._websocket.client) { that._websocket.client.subscriptions = [] for (var i = 0; i < that._websocket.subscriptions.length; i++) { var subs = that._websocket.subscriptions[i] var result = that._websocket.client.subscribe(subs.destination, subs.callback, subs.header) console.log('重新订阅', subs, result) } that._processQueue(that._websocket.client) that._websocket.clientAviable = true }
ws.onopen && ws.onopen() })
wx.onSocketMessage(function (res) { console.log('onSocketMessage事件:', res) ws.onmessage && ws.onmessage(res) })
wx.onSocketClose(function(res){ console.log('onSocketClose 事件,websocket断掉了:', res) that._websocket.clientAviable = false if(that._websocket.client != null){ setTimeout(that._connectSocket, that._websocket.client.reconnect_delay) } })
var Stomp = require('./utils/stomp.js').Stomp;
var client = Stomp.over(ws);
var headers = {} var connectCallback = function (frame) { console.log('STOMP client connect successed', frame)
that._processQueue(client)
that._websocket.client = client that._websocket.clientAviable = true
if (param && param.success){ param.success() } } var errorCallback = function (e) { console.log('STOMP connect error callback ', e) } client.debug = function(str){ console.log(str) } client.heartbeat.outgoing = 20000 client.heartbeat.incoming = 20000 client.reconnect_delay = 3000 client.connect(headers, connectCallback, errorCallback) },
onLaunch: function () { let that = this this._login({ tokenGot: function(){ console.log("login successed.", that.globalData.token) that.connectWebsocket() wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { wx.getUserInfo({ success: res => { that.globalData.userInfo = res.userInfo } }) } } })
} }) },
_login: function (param){ let that = this wx.login({ success(res) { if (res.code) { wx.request({ url: BASEURL + '/auth/login', method: 'POST', data: { code: res.code }, success: function (res) { console.log("after post login data to server", res.data) that.globalData.token = res.data.token that.globalData.user = res.data.user if (param && param.tokenGot) { console.log("after set token") param.tokenGot(res.data.token) } } }) } else { console.log('登录失败!' + res.errMsg) } } }) },
request: function(param) { param.url = BASEURL + param.url if (this.globalData.token) { this._requestWithToken(param) } else { let that = this this._login({ tokenGot: function () { that._requestWithToken(param) } }) } }, _requestWithToken: function (param) { var header = param.header if (header) {
} else { header = {} } header['Authorization'] = 'Bearer ' + this.globalData.token param['header'] = header let successCallback = param.success var resultHandler = function (res) { if (res.statusCode >= 200 && res.statusCode < 300) { if (successCallback) { successCallback(res) } } else { console.log("server error", res) var error = {} error.code = res.statusCode if (res.statusCode >= 400 && res.statusCode < 500) { error.message = "没找到资源,可能已删除" } else if (res.statusCode >= 500 && res.statusCode < 600) { error.message = "服务器出错了,请稍后再试" } else { error.message = "未知错误" } if (param.invalidResponse) { param.invalidResponse(error, res) } } } param['success'] = resultHandler console.log("request " + param.url + " with token " + this.globalData.token) wx.request(param) }, globalData: { token: null } })
|