-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·165 lines (147 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const ws = require('ws');
class connection extends ws {
constructor(room = 'test', human = 0, uri = 'wss://euphoria.io', options = { origin: 'https://euphoria.io'}, ...callback) {
options.origin = options.origin ? options.origin : 'https://euphoria.io';
super(`${uri}/room/${room}/ws?h=${human}`, options);
this.cookie = '';
this.once('upgrade', res => {
this.cookie = res.headers['set-cookie'][0];
});
// Setting the basics for the connection
this.on('open', () => {
this.on('message', this.handleMsg);
// Setting default behaviour keep-open behaviour.
this.on('ping-event', data => {
// any heartbeat should be riding on this event
this.send(JSON.stringify({
type: 'ping-reply',
data: {time: data.data.time}
}));
});
});
// The snapshot event will happen right after opening and delivers relevant information, as such we can use it for the callback. Should not be a once because it also happens on reconnect.
this.on('snapshot-event', raw => {
const data = raw.data;
this._identity = data.identity;
this._version = data.version;
this._session_id = data.session_id;
this.emit('ready');
callback.forEach(f => f(raw));
});
}
handleMsg(data) {
// credit to ArkaneMoose https://github.com/ArkaneMoose/EuphoriaJS/blob/master/connection.js#L19
// formated as https://github.com/websockets/ws/blob/5d90141505dc41c129ab5d5228e37d49979d7541/lib/event-target.js#L87
// console.log(data);
let dt = {};
try {
dt = JSON.parse(data);
} catch (e) {
this.emit('error', new Error('invalid JSON'));
}
if(dt.type)
this.emit(dt.type, dt);
else
this.emit('error', new Error('missing type'));
}
who(...callback) {
this.send(JSON.stringify({
type: 'who',
}));
this.once('who-reply', data => {
callback.forEach(f => f(data));
});
}
download(number = 100, before = null, ...callback) {
this.once('log-reply', data => {
callback.forEach(f => f(data));
number -= number % 1000;
if(data.before && number)
this.download(number-1000, data.before, ...callback);
});
this.send(JSON.stringify({
type: 'log',
data: {
n: number % 1000 ? number % 1000 : number,
before: before ? before : void(0)
}
}));
}
downloadAll(downloadCallback, ...callback) {
this.download(100, null, downloadCallback);
this.on('log-reply', recurse);
function recurse(data){
if(data.data.log[0]){
this.download(1000, data.data.log[0].id, downloadCallback);
} else {
() => this.removeListener('log-reply', recurse);
callback.forEach(f => f(data));
}
}
}
nick(nick = '<><', ...callback) {
this.send(JSON.stringify({
type: 'nick',
data: {name: nick}
}));
this.once('nick-reply', data => {
callback.forEach(f => f(data));
});
}
pm(user_id, ...callback) {
this.send(JSON.stringify({
type: 'pm-initiate',
data: {user_id: user_id}
}));
this.once('pm-initiate-reply', data => {
callback.forEach(f => f(data));
});
}
grant_manager(user_id, ...callback) {
this.send(JSON.stringify({
type: 'grant-manager',
data: {user_id: user_id}
}));
this.once('grant-manager-reply', data => {
callback.forEach(f => f(data));
});
}
post(text, parent, ...callback) {
this.send(JSON.stringify({
type: 'send',
data: {content: text, parent: parent}
}));
this.once('send-reply', data => {
callback.forEach(f => f(data));
});
}
login(namespace, id, password, ...callback) {
this.send(JSON.stringify({
type: 'login',
data: {namespace: namespace, id: id, password: password}
}));
this.once('login-reply', data => {
callback.forEach(f => f(data));
});
}
registerAccount(namespace, id, password, ...callback) {
this.send(JSON.stringify({
type: 'register-account',
data: {namespace: namespace, id: id, password: password}
}));
this.once('register-account-reply', data => {
callback.forEach(f => f(data));
});
}
ping(time, ...callback) {
this.send(JSON.stringify({
type: 'ping',
data: {time: time}
}));
this.once('ping-reply', data => {
callback.forEach(f => f(data));
});
}
}
module.exports = connection;