-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildSession.ts
More file actions
217 lines (187 loc) · 6.09 KB
/
buildSession.ts
File metadata and controls
217 lines (187 loc) · 6.09 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import * as io from "socket.io-client";
import {Context} from "../../api/context";
import {Session, SessionBuilder} from "../../api/session";
import {convert2basic} from "../../helper/utils";
import {BasicMessageFrom, MessageFrom, Receiver, SystemMessageFrom, YourselfMessageFrom} from "../../model/messages";
import {APIOptions, Callback2, Handler1, Handler2, Handler3, LoginResult} from "../../model/models";
import {ContextImpl} from "../context/context";
import {SessionImpl} from "./session";
class SessionBuilderImpl implements SessionBuilder {
private friends: Array<Handler2<string, BasicMessageFrom>>;
private groups: Array<Handler3<string, string, BasicMessageFrom>>;
private rooms: Array<Handler3<string, string, BasicMessageFrom>>;
private passengers: Array<Handler2<string, BasicMessageFrom>>;
private strangers: Array<Handler2<string, BasicMessageFrom>>;
private friendonlines: Array<Handler1<string>>;
private friendofflines: Array<Handler1<string>>;
private strangeronlineds: Array<Handler1<string>>;
private strangerofflines: Array<Handler1<string>>;
private systems: Array<Handler1<SystemMessageFrom>>;
private yourselfs: Array<Handler1<YourselfMessageFrom>>;
private acks: Array<Handler2<number, number>>;
private apiOptions: APIOptions;
private authdata: any;
constructor(apiOptions: APIOptions, authdata: {}) {
this.apiOptions = apiOptions;
this.authdata = authdata;
this.friends = [];
this.groups = [];
this.rooms = [];
this.passengers = [];
this.strangers = [];
this.friendonlines = [];
this.friendofflines = [];
this.strangeronlineds = [];
this.strangerofflines = [];
this.systems = [];
this.yourselfs = [];
this.acks = [];
}
public setNotifyAll(enable: boolean): SessionBuilder {
this.authdata.notifyAll = enable;
return this;
}
public setInstallation(installation: string): SessionBuilder {
this.authdata.install = installation;
return this;
}
public onFriendMessage(handler: Handler2<string, BasicMessageFrom>): SessionBuilder {
this.friends.push(handler);
return this;
}
public onGroupMessage(handler: Handler3<string, string, MessageFrom>): SessionBuilder {
this.groups.push(handler);
return this;
}
public onRoomMessage(handler: Handler3<string, string, MessageFrom>): SessionBuilder {
this.rooms.push(handler);
return this;
}
public onPassengerMessage(handler: Handler2<string, MessageFrom>): SessionBuilder {
this.passengers.push(handler);
return this;
}
public onStrangerMessage(handler: Handler2<string, MessageFrom>): SessionBuilder {
this.strangers.push(handler);
return this;
}
public onFriendOnline(handler: Handler1<string>): SessionBuilder {
this.friendonlines.push(handler);
return this;
}
public onFriendOffline(handler: Handler1<string>): SessionBuilder {
this.friendofflines.push(handler);
return this;
}
public onStrangerOnline(handler: Handler1<string>): SessionBuilder {
this.strangeronlineds.push(handler);
return this;
}
public onStrangerOffline(handler: Handler1<string>): SessionBuilder {
this.strangerofflines.push(handler);
return this;
}
public onSystemMessage(handler: Handler1<SystemMessageFrom>): SessionBuilder {
this.systems.push(handler);
return this;
}
public onAck(handler: Handler2<number, number>): SessionBuilder {
this.acks.push(handler);
return this;
}
public onYourself(handler: Handler1<YourselfMessageFrom>): SessionBuilder {
this.yourselfs.push(handler);
return this;
}
public ok(callback: Callback2<Session, Context>) {
const url = `${this.apiOptions.server}/chat`;
const socket = io.connect(url, {
query: `auth=${JSON.stringify(this.authdata)}`,
transports: ["websocket"],
});
socket.once("login", (result) => {
const foo = result as LoginResult;
if (foo.success) {
const session = new SessionImpl(socket, foo.id);
const ctx = new ContextImpl(this.apiOptions, result.id);
callback(null, session, ctx);
} else {
callback(new Error(`error: ${foo.error}`), null, null);
}
});
socket.on("message", (income) => {
const msg = income as MessageFrom;
const basicmsg = convert2basic(msg);
switch (msg.from.type) {
case Receiver.ACTOR:
for (const it of this.friends) {
it(msg.from.id, basicmsg);
}
break;
case Receiver.GROUP:
for (const it of this.groups) {
it(msg.from.gid, msg.from.id, basicmsg);
}
break;
case Receiver.ROOM:
for (const it of this.rooms) {
it(msg.from.gid, msg.from.id, basicmsg);
}
break;
case Receiver.PASSENGER:
for (const it of this.passengers) {
it(msg.from.id, basicmsg);
}
break;
case Receiver.STRANGER:
for (const it of this.strangers) {
it(msg.from.id, basicmsg);
}
break;
default:
break;
}
});
socket.on("online", (onlineid) => {
for (const it of this.friendonlines) {
it(onlineid as string);
}
});
socket.on("offline", (offlineid) => {
for (const it of this.friendofflines) {
it(offlineid as string);
}
});
socket.on("online_x", (onlineid) => {
for (const it of this.strangeronlineds) {
it(onlineid as string);
}
});
socket.on("offline_x", (offlineid) => {
for (const it of this.strangerofflines) {
it(offlineid as string);
}
});
socket.on("sys", (income) => {
const msg = income as BasicMessageFrom;
for (const it of this.systems) {
it(msg);
}
});
socket.on("yourself", (income) => {
const msg = income as YourselfMessageFrom;
for (const it of this.yourselfs) {
it(msg);
}
});
socket.on("ack", (income) => {
const msg = income as { ack: number, ts: number };
for (const it of this.acks) {
it(msg.ack, msg.ts);
}
});
}
}
export {
SessionBuilderImpl,
};