nanka iroiro
This commit is contained in:
parent
f93eb00207
commit
4e5545af38
17 changed files with 472 additions and 205 deletions
18
src/web/app/common/scripts/home-stream.js
Normal file
18
src/web/app/common/scripts/home-stream.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
'use strict';
|
||||
|
||||
import Stream from './stream';
|
||||
|
||||
/**
|
||||
* Home stream connection
|
||||
*/
|
||||
class Connection extends Stream {
|
||||
constructor(me) {
|
||||
super('', {
|
||||
i: me.token
|
||||
});
|
||||
|
||||
this.on('i_updated', me.update);
|
||||
}
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
@ -1,42 +1,22 @@
|
|||
const ReconnectingWebSocket = require('reconnecting-websocket');
|
||||
import * as riot from 'riot';
|
||||
import CONFIG from './config';
|
||||
'use strict';
|
||||
|
||||
class Connection {
|
||||
import Stream from './stream';
|
||||
|
||||
/**
|
||||
* Messaging stream connection
|
||||
*/
|
||||
class Connection extends Stream {
|
||||
constructor(me, otherparty) {
|
||||
// BIND -----------------------------------
|
||||
this.onOpen = this.onOpen.bind(this);
|
||||
this.onMessage = this.onMessage.bind(this);
|
||||
this.close = this.close.bind(this);
|
||||
// ----------------------------------------
|
||||
super('messaging', {
|
||||
i: me.token,
|
||||
otherparty
|
||||
});
|
||||
|
||||
this.event = riot.observable();
|
||||
this.me = me;
|
||||
|
||||
const host = CONFIG.apiUrl.replace('http', 'ws');
|
||||
this.socket = new ReconnectingWebSocket(`${host}/messaging?i=${me.token}&otherparty=${otherparty}`);
|
||||
this.socket.addEventListener('open', this.onOpen);
|
||||
this.socket.addEventListener('message', this.onMessage);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
this.socket.send(JSON.stringify({
|
||||
i: this.me.token
|
||||
}));
|
||||
}
|
||||
|
||||
onMessage(message) {
|
||||
try {
|
||||
const msg = JSON.parse(message.data);
|
||||
if (msg.type) this.event.trigger(msg.type, msg.body);
|
||||
} catch(e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this.socket.removeEventListener('open', this.onOpen);
|
||||
this.socket.removeEventListener('message', this.onMessage);
|
||||
this.on('_connected_', () => {
|
||||
this.send({
|
||||
i: me.token
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
14
src/web/app/common/scripts/server-stream.js
Normal file
14
src/web/app/common/scripts/server-stream.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
'use strict';
|
||||
|
||||
import Stream from './stream';
|
||||
|
||||
/**
|
||||
* Server stream connection
|
||||
*/
|
||||
class Connection extends Stream {
|
||||
constructor() {
|
||||
super('server');
|
||||
}
|
||||
}
|
||||
|
||||
export default Connection;
|
||||
|
|
@ -5,10 +5,10 @@ import * as riot from 'riot';
|
|||
import CONFIG from './config';
|
||||
|
||||
/**
|
||||
* Home stream connection
|
||||
* Misskey stream connection
|
||||
*/
|
||||
class Connection {
|
||||
constructor(me) {
|
||||
constructor(endpoint, params) {
|
||||
// BIND -----------------------------------
|
||||
this.onOpen = this.onOpen.bind(this);
|
||||
this.onClose = this.onClose.bind(this);
|
||||
|
|
@ -20,16 +20,19 @@ class Connection {
|
|||
riot.observable(this);
|
||||
|
||||
this.state = 'initializing';
|
||||
this.me = me;
|
||||
this.buffer = [];
|
||||
|
||||
const host = CONFIG.apiUrl.replace('http', 'ws');
|
||||
this.socket = new ReconnectingWebSocket(`${host}?i=${me.token}`);
|
||||
const query = params
|
||||
? Object.keys(params)
|
||||
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
|
||||
.join('&')
|
||||
: null;
|
||||
|
||||
this.socket = new ReconnectingWebSocket(`${host}/${endpoint}${query ? '?' + query : ''}`);
|
||||
this.socket.addEventListener('open', this.onOpen);
|
||||
this.socket.addEventListener('close', this.onClose);
|
||||
this.socket.addEventListener('message', this.onMessage);
|
||||
|
||||
this.on('i_updated', me.update);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -137,8 +137,8 @@
|
|||
this.connection = new MessagingStreamConnection(this.I, this.user.id);
|
||||
|
||||
this.on('mount', () => {
|
||||
this.connection.event.on('message', this.onMessage);
|
||||
this.connection.event.on('read', this.onRead);
|
||||
this.connection.on('message', this.onMessage);
|
||||
this.connection.on('read', this.onRead);
|
||||
|
||||
document.addEventListener('visibilitychange', this.onVisibilitychange);
|
||||
|
||||
|
|
@ -153,8 +153,8 @@
|
|||
});
|
||||
|
||||
this.on('unmount', () => {
|
||||
this.connection.event.off('message', this.onMessage);
|
||||
this.connection.event.off('read', this.onRead);
|
||||
this.connection.off('message', this.onMessage);
|
||||
this.connection.off('read', this.onRead);
|
||||
this.connection.close();
|
||||
|
||||
document.removeEventListener('visibilitychange', this.onVisibilitychange);
|
||||
|
|
@ -174,10 +174,10 @@
|
|||
|
||||
this.messages.push(message);
|
||||
if (message.user_id != this.I.id && !document.hidden) {
|
||||
this.connection.socket.send(JSON.stringify({
|
||||
this.connection.send({
|
||||
type: 'read',
|
||||
id: message.id
|
||||
}));
|
||||
});
|
||||
}
|
||||
this.update();
|
||||
|
||||
|
|
@ -239,10 +239,10 @@
|
|||
if (document.hidden) return;
|
||||
this.messages.forEach(message => {
|
||||
if (message.user_id !== this.I.id && !message.is_read) {
|
||||
this.connection.socket.send(JSON.stringify({
|
||||
this.connection.send({
|
||||
type: 'read',
|
||||
id: message.id
|
||||
}));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue