aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/index.js
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-06-30 23:31:03 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:53 +0300
commit61523ca4d0eb3a718e9b029bd3e121f3b539b3b1 (patch)
treeedeb7b90c62ae4d36233fb8e6cc7f1400f0efd1d /vnext/src/index.js
parentfd7dd62c2f08ec993ac2c30f537a53d8e37cff24 (diff)
WebSockets
Diffstat (limited to 'vnext/src/index.js')
-rw-r--r--vnext/src/index.js56
1 files changed, 52 insertions, 4 deletions
diff --git a/vnext/src/index.js b/vnext/src/index.js
index 2bade756..1eb01eae 100644
--- a/vnext/src/index.js
+++ b/vnext/src/index.js
@@ -23,7 +23,7 @@ class App extends React.Component {
super(props);
let params = qs.parse(window.location.search)
if (params.hash) {
- localStorage.visitor = JSON.stringify({uid: 0, hash: params.hash});
+ localStorage.visitor = JSON.stringify({ uid: 0, hash: params.hash });
window.history.replaceState({}, document.title, `${window.location.protocol}//${window.location.host}${window.location.pathname}`)
}
this.state = {
@@ -32,10 +32,58 @@ class App extends React.Component {
hash: params.hash || ''
}
};
+ this.pm = React.createRef();
+ this.thread = React.createRef();
+ }
+
+ initWS = () => {
+ const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
+ 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 = () => {
+ 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);
+ 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, uid} = this.state.visitor;
+ const { hash, uid } = this.state.visitor;
+ this.initWS();
if (uid === 0 && hash) {
me().then(visitor => this.auth(visitor))
}
@@ -92,10 +140,10 @@ class App extends React.Component {
} />
<Route exact path="/post" render={(props) => <Post visitor={user} {...props} />} />
<Route exact path="/pm" render={(props) => <Contacts visitor={user} {...props} />} />
- <Route exact path="/pm/:user" render={(props) => <Chat visitor={user} {...props} />} />
+ <Route exact path="/pm/:user" render={(props) => <Chat ref={this.pm} visitor={user} {...props} />} />
<Route exact path="/:user" render={(props) => <Blog visitor={user} {...props} />} />
<Route exact path="/tag/:tag" render={(props) => <Tag visitor={user} {...props} />} />
- <Route exact path="/:user/:mid" render={(props) => <Thread visitor={user} {...props} />} />
+ <Route exact path="/:user/:mid" render={(props) => <Thread ref={this.thread} visitor={user} {...props} />} />
</Switch>
</section>
<aside id="column">