-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
46 lines (42 loc) · 1.22 KB
/
server.ts
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
import {
Server,
ClientSocketHandler,
IClientSocketHandler,
} from "../../TypedServer";
import * as io from "socket.io";
import {
ChatServerInfo,
MyRootServer,
ChatSocket,
Req,
Res,
runtimeSchema,
} from "./common";
class ChatServer extends Server<ChatServerInfo> {
constructor(ioServer: MyRootServer) {
super(runtimeSchema);
this.listen(ioServer.of("/chat"));
}
onConnection(socket: ChatSocket) {
// to reject connection, return null
return new ChatClientHandler(socket);
}
}
/**
* one of these will be created for every socket connection
*/
class ChatClientHandler extends ClientSocketHandler<ChatServerInfo>
implements IClientSocketHandler<ChatServerInfo> {
async postMessage(
message: Req<"postMessage">,
): Promise<Res<"postMessage">> {
this.socket.nsp.emit("chatMessage", {
...message,
sender: this.socket.id,
// foo: "bar" // Object literal may only specify known properties, and 'foo' does not exist in type 'ChatMessage'.
});
return "ok";
// return "ook" // [ts] Type '"ook"' is not assignable to type '"ok"'.
}
}
new ChatServer(io(8001) as MyRootServer);