使用Java中的推送程序库连接到WebSocket

时间:2018-08-09 14:39:08

标签: javascript java sockets websocket pusher

我正在尝试连接到站点opskins.com上的Websocket,在其他站点上,我通过Chrome调试器(f12)->网络-> WS收到了诸如哈希码,主机名等信息,但在opskins缺少此信息,尽管我确定他们使用的是websockets。
我在其他站点中创建推送对象的代码:

    socketHashToken = "c0eef4118084f8164bec65e6253bf195"; // Value from debugger

    PusherOptions options = new PusherOptions();
    options.setEncrypted(true);
    options.setWsPort(443);
    options.setWssPort(443);
    options.setHost("HOSTNAME");

    Pusher pusher = new Pusher(socketHashToken, options);

也许下一个代码将无用,但有人建议我在以下.js文件中查找所有信息,但我找不到正确的值或其他内容:

var Notifier={"_ws":null,"_connectWait":0,"_listeners":

{},"_lastTabNotify":0,"_tabId":Math.floor(Math.random()*1000000000000),"_clientQueryTimeout":null,"_listenerQueryTimeout":null,"_clientQueryCallback":null,"_listenerQueryCallback":null,"_lastInit":0,"_unloadClaimed":0,"_pakoLoaded":false,"connected":false,"reconnecting":false,"addEventListener":function(event,listener){this._listeners[event]=this._listeners[event]||[];this._listeners[event].push(listener);this._init();},"removeEventListener":function(event,listener){if(!this._listeners[event]){this._init();return false;}
var idx=this._listeners[event].indexOf(listener);if(idx==-1){this._init();return false;}
this._listeners[event].splice(idx,1);this._init();return true;},"removeAllListeners":function(event){delete this._listeners[event];this._init();},"hasEventListener":function(event,listener){return this._listeners[event]&&this._listeners[event].indexOf(listener)!=-1;},"isConnected":function(){return!!(this._ws&&this._ws.readyState==WebSocket.OPEN);},"_emit":function(event,data){(this._listeners[event]||[]).forEach(function(listener){listener(data);});if(event=='balance'){switch(data.currency_type){case 1:$('.op-count').html(formatCoins(data.coins).replace('$',''));break;case 2:$('.op-credits-count').html(formatCredits(data.coins));break;}}},"_emitCrossTabs":function(type,data){},"_hasListeners":function(){for(var i in this._listeners){if(this._listeners.hasOwnProperty(i)&&this._listeners[i].length>0){return true;}}
return false;},"_sendListenerList":function(){if(this._ws&&this._ws.readyState==WebSocket.OPEN){var self=this;var listeners=Object.keys(self._listeners).filter(function(listener){return self._listeners[listener].length>0;});self._ws.send(JSON.stringify({"type":"listener_list","listeners":listeners}));}},"_isLocallyConnected":function(){return(this.reconnecting||(this._ws&&(this._ws.readyState==WebSocket.CONNECTING||this._ws.readyState==WebSocket.OPEN)));},"_init":function(){this._lastInit=Date.now();if(typeof g_UID==='undefined'||!g_UID){return;}
var self=this;if(this._isLocallyConnected()){if(!this._hasListeners()&&this._isLocallyConnected()){console.log("Closing WebSocket connection");self._ws.close(1000,"All listeners removed");}else{self._sendListenerList();}
return;}
if(!self._hasListeners()){return;}
doConnect();function doConnect(){self._emitCrossTabs('client',{"connected":false});console.log("Connecting to Notifier...");self._ws=new WebSocket(location.protocol.replace('http','ws')+"//"+getBaseDomain()+"/notifier","notify-v1");self._ws.binaryType='arraybuffer';self._ws.addEventListener("close",function(event){self.connected=false;console.log("WebSocket connection closed: "+event.code);self._emit("disconnected",{"code":event.code});self._emitCrossTabs("disconnected",{"code":event.code});if(event.code!=1000){reconnect();}});self._ws.addEventListener("open",function(event){self.connected=true;console.log("WebSocket connection established successfully");self._connectWait=0;self._emit("connected");self._emitCrossTabs("connected");self._sendListenerList();if(self._pakoLoaded&&window.pako&&!localStorage.disableWebSocketCompression){console.log("Notifier switching to deflate compression");self._ws.send(JSON.stringify({"type":"switch_compression","compression_type":"deflate"}));}else if(!self._pakoLoaded&&!localStorage.disableWebSocketCompression){$.ajax({"method":"GET","url":"/js/pako_inflate.min.js","cache":true,"success":function(){if(window.pako){console.log("Notifier loaded Pako; switching to deflate compression");self._pakoLoaded=true;self._ws.send(JSON.stringify({"type":"switch_compression","compression_type":"deflate"}));}}});}});self._ws.addEventListener("message",function(event){try{var msg;if(typeof event.data==='string'){msg=JSON.parse(event.data);}else{msg=JSON.parse(pako.inflate(event.data,{"to":"string"}));}
var type=msg.type;if(!type){console.warn("Got msg with no type: "+event.data);return;}
self._emitCrossTabs("notification",msg);delete msg.type;self._emit(type,msg);}catch(ex){console.warn(ex);}});function reconnect(){if(self._connectWait==0){self._connectWait=1000;}else{self._connectWait=Math.min(30000,self._connectWait*2);}
this.reconnecting=true;setTimeout(function(){self.reconnecting=false;self._ws=null;self._init();},self._connectWait);}}}};cleanUpNotifierStorage();function cleanUpNotifierStorage(){Object.keys(localStorage).forEach(function(item){var match=item.match(/^notifier_event_(\d+)_\d+$/);if(match&&Date.now()-match[1]>=30000){delete localStorage[item];}});}  

我希望您可以通过pusher库进行进一步的连接,或者通过其他不使用哈希码的方式(例如通过socket.io库或其他方式)连接到Websocket的方式,来帮助您查找哈希码。

0 个答案:

没有答案