diff options
-rw-r--r-- | vnext/src/App.js | 67 |
1 files changed, 24 insertions, 43 deletions
diff --git a/vnext/src/App.js b/vnext/src/App.js index ab59fc10..5397a4b7 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -37,60 +37,41 @@ export default class App extends React.Component { this.thread = React.createRef(); } - initWS = () => { - const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + initES = () => { const params = { hash: this.state.visitor.hash }; - let url = `${proto}//api.juick.com/ws/?${qs.stringify(params)}`; - this.ws = new WebSocket(url); - this.ws.onopen = () => { + let url = new URL(`https://api.juick.com/events?${qs.stringify(params)}`); + this.es = new EventSource(url); + this.es.onopen = () => { console.log('online'); }; - this.ws.onclose = () => { - console.log('offline'); - this.ws = false; - setTimeout(function () { - this.initWS(); - }, 2000); - }; - this.ws.onmessage = (msg) => { - if (msg.data == ' ') { - this.ws.send(' '); - } else { - try { - var jsonMsg = JSON.parse(msg.data); - console.log('data: ' + msg.data); - // refresh server visitor state (unread counters) - me().then(visitor => { - this.setState({ - visitor: visitor - }); + this.es.onmessage = (msg) => { + try { + var jsonMsg = JSON.parse(msg.data); + console.log('data: ' + msg.data); + // refresh server visitor state (unread counters) + me().then(visitor => { + this.setState({ + visitor: visitor }); - if (jsonMsg.service) { - return; - } - if (!jsonMsg.mid) { - this.pm.current.onMessage(jsonMsg); - } - if (jsonMsg.rid && this.thread.current) { - this.thread.current.onReply(jsonMsg); - } - } catch (err) { - console.log(err); + }); + if (jsonMsg.service) { + return; + } + if (!jsonMsg.mid) { + this.pm.current.onMessage(jsonMsg); + } + if (jsonMsg.rid && this.thread.current) { + this.thread.current.onReply(jsonMsg); } + } catch (err) { + console.log(err); } }; - setInterval(this.wsSendKeepAlive, 90000); - } - - wsSendKeepAlive = () => { - if (this.ws) { - this.ws.send(' '); - } } componentDidMount() { const { hash } = this.state.visitor; - this.initWS(); + this.initES(); if (hash) { me().then(visitor => this.auth(visitor)); } |