diff options
author | Vitaly Takmazov | 2018-10-24 14:38:00 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-13 10:37:53 +0300 |
commit | 885d573d226ed70205e1cbe1fc3522a1296d25bc (patch) | |
tree | 3257eb50ffe474850500110c5e660b2a612f4732 /vnext/src/App.js | |
parent | e2472c77ad37c11996093418d2ee1a5e88db938d (diff) |
Websocker -> EventSource
Diffstat (limited to 'vnext/src/App.js')
-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)); } |