2020-07-21 17:09:24 +08:00
// importScripts('sockjs.js');
2020-03-08 19:40:46 +08:00
// import { getToken } from '@/utils/auth';
// import { getBaseUrl } from '@/utils/baseUrl';
// import SockJS from 'sockjs-client';
// const websocketUrl = 'ws://test.joylink.club/jlcloud/joylink-websocket?token=';
// const websocketUrl = `http://192.168.3.6:9000/joylink-websocket?token=`;
let websocketUrl = null ;
let token = null ;
2020-07-21 17:09:24 +08:00
var StompClient = function ( headers , token ) {
const scope = this ;
2020-03-08 19:40:46 +08:00
this . url = websocketUrl + token ;
this . headers = headers || { } ;
this . connect ( ) ;
} ;
StompClient . prototype = {
socket : null ,
clientIns : null ,
subscribeMap : null ,
url : '' ,
status : false ,
sockStatus : 0 ,
headers : {
// 'X-Token': getToken()
} ,
count : 0 ,
topic : '' ,
onmessage : null ,
checkTimer : null ,
// 连接服务端
connect ( ) {
return new Promise ( ( resolve , reject ) => {
try {
// 断开已有连接
if ( this . clientIns && this . clientIns . connected ) {
this . clientIns . disconnect ( ) ;
this . clientIns = null ;
}
2020-07-21 11:01:02 +08:00
// // 建立连接对象(还未发起连接)
// this.socket = new SockJS(websocketUrl + token);
//
// // 获取 STOMP 子协议的客户端对象
// this.clientIns = Stomp.over(this.socket);
2020-03-08 19:40:46 +08:00
2020-07-21 11:01:02 +08:00
this . websocket = new WebSocket ( this . url . replace ( /https/ , 'wss' ) . replace ( /http/ , 'ws' ) ) ;
this . clientIns = Stomp . over ( this . websocket ) ;
2020-03-08 19:40:46 +08:00
this . closeStompDebug ( ) ;
// 向服务器发起websocket连接并发送CONNECT帧
this . clientIns . connect ( { 'X-Token' : token } , ( ) => {
console . info ( '连接成功.' ) ;
this . count = 0 ;
this . status = true ;
// 恢复订阅
if ( this . topic && this . onmessage ) {
this . unsubscribe ( this . topic ) ;
this . subscribe ( this . topic , this . onmessage , this . headers ) ;
}
resolve ( this ) ;
} , ( ) => {
if ( this . checkTimer ) {
clearInterval ( this . checkTimer ) ;
this . checkTimer = null ;
}
} ) ;
} catch ( err ) {
reject ( err ) ;
}
} ) ;
} ,
logOut ( err ) {
if ( err . code == 50008 || err . code == 50014 ) {
this . url = websocketUrl + token ;
this . status = false ;
this . count ++ ;
this . reconnect ( this . count ) ;
}
} ,
// 恢复链接
reconnect ( count ) {
console . info ( ` 尝试第 ${ count || 1 } 次连接. ` ) ;
this . connect ( ) . then ( ( ) => { } ) . catch ( ( ) => {
this . count ++ ;
this . reconnect ( this . count ) ;
} ) ;
} ,
closeStompDebug ( ) {
if ( this . clientIns ) {
this . clientIns . debug = undefined ;
}
} ,
// 订阅指定的topic
subscribe ( topic , onmessage , headers ) {
this . topic = topic ;
this . onmessage = onmessage ;
this . headers = headers ;
if ( this . status ) {
if ( ! this . subscribeMap ) {
this . subscribeMap = new Map ( ) ;
}
try {
var subscription = this . subscribeMap . get ( topic ) ;
if ( ! subscription ) {
subscription = this . clientIns . subscribe ( topic , onmessage , headers ) ; // 接收消息通过 subscribe() 方法实现
this . subscribeMap . set ( topic , subscription ) ;
}
} catch ( err ) {
setTimeout ( ( ) => {
this . subscribe ( topic , onmessage , headers ) ;
} , 300 ) ;
}
} else {
setTimeout ( ( ) => {
this . subscribe ( topic , onmessage , headers ) ;
} , 300 ) ;
}
} ,
unsubscribe ( topic ) {
if ( this . subscribeMap ) {
const subscription = this . subscribeMap . get ( topic ) ;
if ( subscription ) {
subscription . unsubscribe ( ) ;
this . subscribeMap . delete ( topic ) ;
console . log ( '取消订阅' ) ;
}
}
} ,
// 发送消息
send ( url , msg ) {
if ( this . status ) {
if ( msg ) {
msg = JSON . stringify ( msg ) ;
}
try {
this . clientIns . send ( url , { } , msg ) ;
} catch ( err ) {
this . status = false ;
this . send ( url , msg ) ;
}
} else {
setTimeout ( ( ) => {
this . send ( url , msg ) ;
} , 300 ) ;
}
} ,
disconnect ( ) {
if ( this . checkTimer ) {
clearInterval ( this . checkTimer ) ;
this . checkTimer = null ;
}
if ( this . clientIns && this . clientIns . connected ) {
this . clientIns . disconnect ( ) ;
this . clientIns = null ;
}
this . status = false ;
console . log ( '断开连接' ) ;
}
} ;
2020-07-21 17:09:24 +08:00
var Byte ; var Client ; var Frame ; var Stomp ;
var _ _hasProp = { } . hasOwnProperty ;
var _ _slice = [ ] . slice ;
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
Byte = {
2020-03-08 19:40:46 +08:00
LF : '\x0A' ,
NULL : '\x00'
2020-07-21 17:09:24 +08:00
} ;
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
Frame = ( function ( ) {
2020-03-08 19:40:46 +08:00
var unmarshallSingle ;
function Frame ( command , headers , body ) {
2020-07-21 17:09:24 +08:00
this . command = command ;
this . headers = headers != null ? headers : { } ;
this . body = body != null ? body : '' ;
2020-03-08 19:40:46 +08:00
}
Frame . prototype . toString = function ( ) {
2020-07-21 17:09:24 +08:00
var lines , name , skipContentLength , value , _ref ;
lines = [ this . command ] ;
skipContentLength = this . headers [ 'content-length' ] === false ;
if ( skipContentLength ) {
delete this . headers [ 'content-length' ] ;
}
_ref = this . headers ;
for ( name in _ref ) {
if ( ! _ _hasProp . call ( _ref , name ) ) continue ;
value = _ref [ name ] ;
lines . push ( '' + name + ':' + value ) ;
}
if ( this . body && ! skipContentLength ) {
lines . push ( 'content-length:' + ( Frame . sizeOfUTF8 ( this . body ) ) ) ;
}
lines . push ( Byte . LF + this . body ) ;
return lines . join ( Byte . LF ) ;
2020-03-08 19:40:46 +08:00
} ;
Frame . sizeOfUTF8 = function ( s ) {
2020-07-21 17:09:24 +08:00
if ( s ) {
return encodeURI ( s ) . match ( /%..|./g ) . length ;
} else {
return 0 ;
}
2020-03-08 19:40:46 +08:00
} ;
unmarshallSingle = function ( data ) {
2020-07-21 17:09:24 +08:00
var body , chr , command , divider , headerLines , headers , i , idx , len , line , start , trim , _i , _j , _len , _ref , _ref1 ;
divider = data . search ( RegExp ( '' + Byte . LF + Byte . LF ) ) ;
headerLines = data . substring ( 0 , divider ) . split ( Byte . LF ) ;
command = headerLines . shift ( ) ;
headers = { } ;
trim = function ( str ) {
return str . replace ( /^\s+|\s+$/g , '' ) ;
} ;
_ref = headerLines . reverse ( ) ;
for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
line = _ref [ _i ] ;
idx = line . indexOf ( ':' ) ;
headers [ trim ( line . substring ( 0 , idx ) ) ] = trim ( line . substring ( idx + 1 ) ) ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
body = '' ;
start = divider + 2 ;
if ( headers [ 'content-length' ] ) {
len = parseInt ( headers [ 'content-length' ] ) ;
body = ( '' + data ) . substring ( start , start + len ) ;
} else {
chr = null ;
for ( i = _j = start , _ref1 = data . length ; start <= _ref1 ? _j < _ref1 : _j > _ref1 ; i = start <= _ref1 ? ++ _j : -- _j ) {
chr = data . charAt ( i ) ;
if ( chr === Byte . NULL ) {
break ;
}
body += chr ;
}
}
return new Frame ( command , headers , body ) ;
2020-03-08 19:40:46 +08:00
} ;
Frame . unmarshall = function ( datas ) {
2020-07-21 17:09:24 +08:00
var data ;
return ( function ( ) {
var _i , _len , _ref , _results ;
_ref = datas . split ( RegExp ( '' + Byte . NULL + Byte . LF + '*' ) ) ;
_results = [ ] ;
for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
data = _ref [ _i ] ;
if ( ( data != null ? data . length : void 0 ) > 0 ) {
_results . push ( unmarshallSingle ( data ) ) ;
}
}
return _results ;
} ) ( ) ;
2020-03-08 19:40:46 +08:00
} ;
Frame . marshall = function ( command , headers , body ) {
2020-07-21 17:09:24 +08:00
var frame ;
frame = new Frame ( command , headers , body ) ;
return frame . toString ( ) + Byte . NULL ;
2020-03-08 19:40:46 +08:00
} ;
return Frame ;
2020-07-21 17:09:24 +08:00
} ) ( ) ;
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
Client = ( function ( ) {
2020-03-08 19:40:46 +08:00
var now ;
function Client ( ws ) {
2020-07-21 17:09:24 +08:00
this . ws = ws ;
this . ws . binaryType = 'arraybuffer' ;
this . counter = 0 ;
this . connected = false ;
this . heartbeat = {
outgoing : 10000 ,
incoming : 10000
} ;
this . maxWebSocketFrameSize = 16 * 1024 ;
this . subscriptions = { } ;
2020-03-08 19:40:46 +08:00
}
Client . prototype . debug = function ( message ) {
2020-07-21 17:09:24 +08:00
var _ref ;
return typeof window !== 'undefined' && window !== null ? ( _ref = window . console ) != null ? _ref . log ( message ) : void 0 : void 0 ;
2020-03-08 19:40:46 +08:00
} ;
now = function ( ) {
2020-07-21 17:09:24 +08:00
if ( Date . now ) {
return Date . now ( ) ;
} else {
return new Date ( ) . valueOf ;
}
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . _transmit = function ( command , headers , body ) {
2020-07-21 17:09:24 +08:00
var out ;
out = Frame . marshall ( command , headers , body ) ;
if ( typeof this . debug === 'function' ) {
this . debug ( '>>> ' + out ) ;
}
while ( true ) {
if ( out . length > this . maxWebSocketFrameSize ) {
this . ws . send ( out . substring ( 0 , this . maxWebSocketFrameSize ) ) ;
out = out . substring ( this . maxWebSocketFrameSize ) ;
if ( typeof this . debug === 'function' ) {
this . debug ( 'remaining = ' + out . length ) ;
}
} else {
return this . ws . send ( out ) ;
}
2020-03-08 19:40:46 +08:00
}
} ;
Client . prototype . _setupHeartbeat = function ( headers ) {
2020-07-21 17:09:24 +08:00
var serverIncoming , serverOutgoing , ttl , v , _ref , _ref1 ;
if ( ( _ref = headers . version ) !== Stomp . VERSIONS . V1 _1 && _ref !== Stomp . VERSIONS . V1 _2 ) {
return ;
}
_ref1 = ( function ( ) {
var _i , _len , _ref1 , _results ;
_ref1 = headers [ 'heart-beat' ] . split ( ',' ) ;
_results = [ ] ;
for ( _i = 0 , _len = _ref1 . length ; _i < _len ; _i ++ ) {
v = _ref1 [ _i ] ;
_results . push ( parseInt ( v ) ) ;
}
return _results ;
} ) ( ) , serverOutgoing = _ref1 [ 0 ] , serverIncoming = _ref1 [ 1 ] ;
if ( ! ( this . heartbeat . outgoing === 0 || serverIncoming === 0 ) ) {
ttl = Math . max ( this . heartbeat . outgoing , serverIncoming ) ;
if ( typeof this . debug === 'function' ) {
this . debug ( 'send PING every ' + ttl + 'ms' ) ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
this . pinger = Stomp . setInterval ( ttl , ( function ( _this ) {
return function ( ) {
_this . ws . send ( Byte . LF ) ;
return typeof _this . debug === 'function' ? _this . debug ( '>>> PING' ) : void 0 ;
} ;
} ) ( this ) ) ;
}
if ( ! ( this . heartbeat . incoming === 0 || serverOutgoing === 0 ) ) {
ttl = Math . max ( this . heartbeat . incoming , serverOutgoing ) ;
if ( typeof this . debug === 'function' ) {
this . debug ( 'check PONG every ' + ttl + 'ms' ) ;
}
return this . ponger = Stomp . setInterval ( ttl , ( function ( _this ) {
return function ( ) {
var delta ;
delta = now ( ) - _this . serverActivity ;
if ( delta > ttl * 2 ) {
if ( typeof _this . debug === 'function' ) {
_this . debug ( 'did not receive server activity for the last ' + delta + 'ms' ) ;
}
return _this . ws . close ( ) ;
}
} ;
} ) ( this ) ) ;
}
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . _parseConnect = function ( ) {
2020-07-21 17:09:24 +08:00
var args , connectCallback , errorCallback , headers ;
args = arguments . length >= 1 ? _ _slice . call ( arguments , 0 ) : [ ] ;
headers = { } ;
switch ( args . length ) {
2020-03-08 19:40:46 +08:00
case 2 :
2020-07-21 17:09:24 +08:00
headers = args [ 0 ] , connectCallback = args [ 1 ] ;
break ;
2020-03-08 19:40:46 +08:00
case 3 :
2020-07-21 17:09:24 +08:00
if ( args [ 1 ] instanceof Function ) {
headers = args [ 0 ] , connectCallback = args [ 1 ] , errorCallback = args [ 2 ] ;
} else {
headers . login = args [ 0 ] , headers . passcode = args [ 1 ] , connectCallback = args [ 2 ] ;
}
break ;
2020-03-08 19:40:46 +08:00
case 4 :
2020-07-21 17:09:24 +08:00
headers . login = args [ 0 ] , headers . passcode = args [ 1 ] , connectCallback = args [ 2 ] , errorCallback = args [ 3 ] ;
break ;
2020-03-08 19:40:46 +08:00
default :
2020-07-21 17:09:24 +08:00
headers . login = args [ 0 ] , headers . passcode = args [ 1 ] , connectCallback = args [ 2 ] , errorCallback = args [ 3 ] , headers . host = args [ 4 ] ;
}
return [ headers , connectCallback , errorCallback ] ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . connect = function ( ) {
2020-07-21 17:09:24 +08:00
var args , errorCallback , headers , out ;
args = arguments . length >= 1 ? _ _slice . call ( arguments , 0 ) : [ ] ;
out = this . _parseConnect . apply ( this , args ) ;
headers = out [ 0 ] , this . connectCallback = out [ 1 ] , errorCallback = out [ 2 ] ;
if ( typeof this . debug === 'function' ) {
this . debug ( 'Opening Web Socket...' ) ;
}
this . ws . onmessage = ( function ( _this ) {
return function ( evt ) {
var arr , c , client , data , frame , messageID , onreceive , subscription , _i , _len , _ref , _results ;
data = typeof ArrayBuffer !== 'undefined' && evt . data instanceof ArrayBuffer ? ( arr = new Uint8Array ( evt . data ) , typeof _this . debug === 'function' ? _this . debug ( '--- got data length: ' + arr . length ) : void 0 , ( ( function ( ) {
var _i , _len , _results ;
_results = [ ] ;
for ( _i = 0 , _len = arr . length ; _i < _len ; _i ++ ) {
c = arr [ _i ] ;
_results . push ( String . fromCharCode ( c ) ) ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
return _results ;
} ) ( ) ) . join ( '' ) ) : evt . data ;
_this . serverActivity = now ( ) ;
if ( data === Byte . LF ) {
if ( typeof _this . debug === 'function' ) {
_this . debug ( '<<< PONG' ) ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
return ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
if ( typeof _this . debug === 'function' ) {
_this . debug ( '<<< ' + data ) ;
}
_ref = Frame . unmarshall ( data ) ;
_results = [ ] ;
for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
frame = _ref [ _i ] ;
switch ( frame . command ) {
case 'CONNECTED' :
if ( typeof _this . debug === 'function' ) {
_this . debug ( 'connected to server ' + frame . headers . server ) ;
}
_this . connected = true ;
_this . _setupHeartbeat ( frame . headers ) ;
_results . push ( typeof _this . connectCallback === 'function' ? _this . connectCallback ( frame ) : void 0 ) ;
break ;
case 'MESSAGE' :
subscription = frame . headers . subscription ;
onreceive = _this . subscriptions [ subscription ] || _this . onreceive ;
if ( onreceive ) {
client = _this ;
messageID = frame . headers [ 'message-id' ] ;
frame . ack = function ( headers ) {
if ( headers == null ) {
headers = { } ;
}
return client . ack ( messageID , subscription , headers ) ;
} ;
frame . nack = function ( headers ) {
if ( headers == null ) {
headers = { } ;
}
return client . nack ( messageID , subscription , headers ) ;
} ;
_results . push ( onreceive ( frame ) ) ;
} else {
_results . push ( typeof _this . debug === 'function' ? _this . debug ( 'Unhandled received MESSAGE: ' + frame ) : void 0 ) ;
}
break ;
case 'RECEIPT' :
_results . push ( typeof _this . onreceipt === 'function' ? _this . onreceipt ( frame ) : void 0 ) ;
break ;
case 'ERROR' :
_results . push ( typeof errorCallback === 'function' ? errorCallback ( frame ) : void 0 ) ;
break ;
default :
_results . push ( typeof _this . debug === 'function' ? _this . debug ( 'Unhandled frame: ' + frame ) : void 0 ) ;
}
}
return _results ;
} ;
} ) ( this ) ;
this . ws . onclose = ( function ( _this ) {
return function ( ) {
var msg ;
msg = 'Whoops! Lost connection to ' + _this . ws . url ;
if ( typeof _this . debug === 'function' ) {
_this . debug ( msg ) ;
}
_this . _cleanUp ( ) ;
return typeof errorCallback === 'function' ? errorCallback ( msg ) : void 0 ;
} ;
} ) ( this ) ;
return this . ws . onopen = ( function ( _this ) {
return function ( ) {
if ( typeof _this . debug === 'function' ) {
_this . debug ( 'Web Socket Opened...' ) ;
}
headers [ 'accept-version' ] = Stomp . VERSIONS . supportedVersions ( ) ;
headers [ 'heart-beat' ] = [ _this . heartbeat . outgoing , _this . heartbeat . incoming ] . join ( ',' ) ;
return _this . _transmit ( 'CONNECT' , headers ) ;
} ;
} ) ( this ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . disconnect = function ( disconnectCallback , headers ) {
2020-07-21 17:09:24 +08:00
if ( headers == null ) {
headers = { } ;
}
this . _transmit ( 'DISCONNECT' , headers ) ;
this . ws . onclose = null ;
this . ws . close ( ) ;
this . _cleanUp ( ) ;
return typeof disconnectCallback === 'function' ? disconnectCallback ( ) : void 0 ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . _cleanUp = function ( ) {
2020-07-21 17:09:24 +08:00
this . connected = false ;
if ( this . pinger ) {
Stomp . clearInterval ( this . pinger ) ;
}
if ( this . ponger ) {
return Stomp . clearInterval ( this . ponger ) ;
}
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . send = function ( destination , headers , body ) {
2020-07-21 17:09:24 +08:00
if ( headers == null ) {
headers = { } ;
}
if ( body == null ) {
body = '' ;
}
headers . destination = destination ;
return this . _transmit ( 'SEND' , headers , body ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . subscribe = function ( destination , callback , headers ) {
2020-07-21 17:09:24 +08:00
var client ;
if ( headers == null ) {
headers = { } ;
}
if ( ! headers . id ) {
headers . id = 'sub-' + this . counter ++ ;
}
headers . destination = destination ;
this . subscriptions [ headers . id ] = callback ;
this . _transmit ( 'SUBSCRIBE' , headers ) ;
client = this ;
return {
id : headers . id ,
unsubscribe : function ( ) {
return client . unsubscribe ( headers . id ) ;
}
} ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . unsubscribe = function ( id ) {
2020-07-21 17:09:24 +08:00
delete this . subscriptions [ id ] ;
return this . _transmit ( 'UNSUBSCRIBE' , {
id : id
} ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . begin = function ( transaction ) {
2020-07-21 17:09:24 +08:00
var client , txid ;
txid = transaction || 'tx-' + this . counter ++ ;
this . _transmit ( 'BEGIN' , {
transaction : txid
} ) ;
client = this ;
return {
id : txid ,
commit : function ( ) {
return client . commit ( txid ) ;
} ,
abort : function ( ) {
return client . abort ( txid ) ;
}
} ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . commit = function ( transaction ) {
2020-07-21 17:09:24 +08:00
return this . _transmit ( 'COMMIT' , {
transaction : transaction
} ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . abort = function ( transaction ) {
2020-07-21 17:09:24 +08:00
return this . _transmit ( 'ABORT' , {
transaction : transaction
} ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . ack = function ( messageID , subscription , headers ) {
2020-07-21 17:09:24 +08:00
if ( headers == null ) {
headers = { } ;
}
headers [ 'message-id' ] = messageID ;
headers . subscription = subscription ;
return this . _transmit ( 'ACK' , headers ) ;
2020-03-08 19:40:46 +08:00
} ;
Client . prototype . nack = function ( messageID , subscription , headers ) {
2020-07-21 17:09:24 +08:00
if ( headers == null ) {
headers = { } ;
}
headers [ 'message-id' ] = messageID ;
headers . subscription = subscription ;
return this . _transmit ( 'NACK' , headers ) ;
2020-03-08 19:40:46 +08:00
} ;
return Client ;
2020-07-21 17:09:24 +08:00
} ) ( ) ;
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
Stomp = {
2020-03-08 19:40:46 +08:00
VERSIONS : {
2020-07-21 17:09:24 +08:00
V1 _0 : '1.0' ,
V1 _1 : '1.1' ,
V1 _2 : '1.2' ,
supportedVersions : function ( ) {
return '1.1,1.0' ;
}
2020-03-08 19:40:46 +08:00
} ,
client : function ( url , protocols ) {
2020-07-21 17:09:24 +08:00
var klass , ws ;
if ( protocols == null ) {
protocols = [ 'v10.stomp' , 'v11.stomp' ] ;
}
klass = Stomp . WebSocketClass || WebSocket ;
ws = new klass ( url , protocols ) ;
return new Client ( ws ) ;
2020-03-08 19:40:46 +08:00
} ,
over : function ( ws ) {
2020-07-21 17:09:24 +08:00
return new Client ( ws ) ;
2020-03-08 19:40:46 +08:00
} ,
Frame : Frame
2020-07-21 17:09:24 +08:00
} ;
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
if ( typeof exports !== 'undefined' && exports !== null ) {
2020-03-08 19:40:46 +08:00
exports . Stomp = Stomp ;
2020-07-21 17:09:24 +08:00
}
Stomp . setInterval = function ( interval , f ) {
return setInterval ( f , interval ) ;
} ;
Stomp . clearInterval = function ( id ) {
return clearInterval ( id ) ;
} ;
2020-03-08 19:40:46 +08:00
let data = null ;
let topic = null ;
let header = null ;
2020-07-21 17:09:24 +08:00
function timedCount ( ) {
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
onmessage = ( e ) => {
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
if ( e . data . type == 'init' ) {
2020-03-08 19:40:46 +08:00
topic = e . data . topic ;
header = { 'X-Token' : e . data . token } ;
2020-07-21 17:09:24 +08:00
token = e . data . token ;
websocketUrl = e . data . baseurl + '/joylink-websocket?token=' ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
if ( e . data == 'connect' ) {
const teststomp = new StompClient ( header , token ) ;
teststomp . subscribe ( topic , callback , header ) ;
2020-03-08 19:40:46 +08:00
}
2020-07-21 17:09:24 +08:00
} ;
function callback ( Response ) {
data = JSON . parse ( Response . body ) ;
// console.log(data);
// if(data.type == 'TrainRun_3D'){
// for (let i=0,leni = data.body.length; i<leni; i++) {
// data.body[i].type = "TRAIN";
// postMessage(data.body[i]);
// }
// }
if ( data . type == 'Device_Load_Destroy_3D' ) {
postMessage ( data ) ;
return ;
}
2020-03-08 19:40:46 +08:00
2020-07-21 17:09:24 +08:00
if ( data . type == 'TrainRun_3D' ) {
postMessage ( data ) ;
return ;
}
// data.type == 'Simulation_DeviceStatus' &&
// if(data.type == 'Simulation_DeviceStatus'){
// for (let i= 0,leni = data.body.length; i<leni; i++) {
// if(data.body[i].deviceType == "TRAIN"){
// postMessage(data.body[i]);
// }
// }
// }
if ( data . type == 'Train_Hmi_3D' ) {
postMessage ( data ) ;
return ;
}
if ( data . type == 'DeviceCtrl_3D' ) {
// console.log(data);
postMessage ( data . body ) ;
return ;
}
if ( data . type == 'Simulation_Reset' ) {
postMessage ( data ) ;
return ;
}
if ( data . body . length > 300 ) {
// console.log(data);
postMessage ( data ) ;
return ;
}
2020-03-25 18:47:14 +08:00
2020-03-08 19:40:46 +08:00
}
}
timedCount ( ) ;