@domain.js/main 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/http/defines.d.ts +6 -13
- package/dist/http/socket.d.ts +12 -2
- package/dist/http/socket.js +22 -3
- package/package.json +1 -1
package/dist/http/defines.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Opt as Sign } from "../deps/signer";
|
|
1
2
|
export interface Cnf {
|
|
2
3
|
proxyIps?: string;
|
|
3
4
|
port?: number;
|
|
@@ -7,15 +8,6 @@ export interface Cnf {
|
|
|
7
8
|
socket?: boolean;
|
|
8
9
|
[propName: string]: any;
|
|
9
10
|
}
|
|
10
|
-
interface Sign {
|
|
11
|
-
signature: string;
|
|
12
|
-
uri: string;
|
|
13
|
-
key: string;
|
|
14
|
-
timestamp: number;
|
|
15
|
-
signMethod: string;
|
|
16
|
-
signVersion: string;
|
|
17
|
-
method: string;
|
|
18
|
-
}
|
|
19
11
|
export interface Profile {
|
|
20
12
|
clientIp: string;
|
|
21
13
|
remoteIp: string;
|
|
@@ -28,10 +20,12 @@ export interface Profile {
|
|
|
28
20
|
revision?: string;
|
|
29
21
|
uuid?: string;
|
|
30
22
|
token?: string;
|
|
31
|
-
sign?: Sign
|
|
23
|
+
sign?: Sign & {
|
|
24
|
+
signature: string;
|
|
25
|
+
};
|
|
32
26
|
isSocket?: boolean;
|
|
33
|
-
/** socket
|
|
34
|
-
|
|
27
|
+
/** socket 的时候加入的房间 */
|
|
28
|
+
roomId?: string;
|
|
35
29
|
}
|
|
36
30
|
export interface HttpCodes {
|
|
37
31
|
[propName: string]: number;
|
|
@@ -45,4 +39,3 @@ export interface Err {
|
|
|
45
39
|
code?: number | string;
|
|
46
40
|
data?: any;
|
|
47
41
|
}
|
|
48
|
-
export {};
|
package/dist/http/socket.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
import { Server } from "socket.io";
|
|
2
|
-
import {
|
|
1
|
+
import { Server, Socket } from "socket.io";
|
|
2
|
+
import { DefaultEventsMap } from "socket.io/dist/typed-events";
|
|
3
|
+
import { Domain, Profile } from "./defines";
|
|
4
|
+
export declare type Listener = Client["emit"] & {
|
|
5
|
+
roomId?: string;
|
|
6
|
+
};
|
|
7
|
+
declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any> & {
|
|
8
|
+
profile?: ReturnType<typeof makeProfile>;
|
|
9
|
+
listener?: Listener;
|
|
10
|
+
};
|
|
11
|
+
declare const makeProfile: (client: Client, token: string, params: any, extra: Profile["extra"]) => Profile;
|
|
3
12
|
export declare function BridgeSocket(io: Server, domain: Domain): void;
|
|
13
|
+
export {};
|
package/dist/http/socket.js
CHANGED
|
@@ -59,10 +59,13 @@ const makeProfile = (client, token, params, extra) => {
|
|
|
59
59
|
function BridgeSocket(io, domain) {
|
|
60
60
|
const subscribe = domain["message.subscribe"];
|
|
61
61
|
const unsubscribe = domain["message.unsubscribe"];
|
|
62
|
+
const entrance = domain["message.entrance"];
|
|
62
63
|
if (!subscribe)
|
|
63
64
|
throw Error("要启用 socket 服务,必须要要有 message.subscribe 方法,用来处理 socket 订阅");
|
|
64
65
|
if (!unsubscribe)
|
|
65
66
|
throw Error("要启用 socket 服务,必须要要有 message.unsubscribe 方法,用来处理 socket 退订");
|
|
67
|
+
if (!entrance)
|
|
68
|
+
throw Error("要启用 socket 服务,必须要要有 message.entrance 方法,用来处理 加入某个房间");
|
|
66
69
|
io.on("connection", (client) => {
|
|
67
70
|
console.log("[%s] connection: client.id: %s", new Date(), client.id);
|
|
68
71
|
client.on("init", async (token, params, extra) => {
|
|
@@ -75,12 +78,11 @@ function BridgeSocket(io, domain) {
|
|
|
75
78
|
Object.assign(client, { profile: makeProfile(client, token, params, extra) });
|
|
76
79
|
if (!client.profile)
|
|
77
80
|
throw new MyError("noAuth", "请先登录");
|
|
78
|
-
const session = await domain["auth.session"](client.profile, {});
|
|
79
81
|
// 创建消息监听函数
|
|
80
82
|
if (!client.listener)
|
|
81
83
|
client.listener = client.emit.bind(client);
|
|
82
84
|
// 向领域注册改用户的监听函数
|
|
83
|
-
|
|
85
|
+
const session = subscribe(client.profile, client.listener);
|
|
84
86
|
client.emit("inited", session);
|
|
85
87
|
}
|
|
86
88
|
catch (e) {
|
|
@@ -91,8 +93,25 @@ function BridgeSocket(io, domain) {
|
|
|
91
93
|
console.error(e);
|
|
92
94
|
}
|
|
93
95
|
});
|
|
96
|
+
client.on("entrance", async (roomId) => {
|
|
97
|
+
try {
|
|
98
|
+
if (!client.profile || !client.listener)
|
|
99
|
+
return;
|
|
100
|
+
const res = await entrance({ roomId, ...client.profile }, client.listener);
|
|
101
|
+
client.profile.roomId = roomId;
|
|
102
|
+
client.listener.roomId = roomId;
|
|
103
|
+
client.emit("entranced", res);
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
if (e instanceof MyError) {
|
|
107
|
+
client.emit("internalError", e.message, e.code || "unknown");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
console.error(e);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
94
113
|
client.use(async ([name, params, responseId], next) => {
|
|
95
|
-
if (name === "init")
|
|
114
|
+
if (name === "init" || name === "entrance")
|
|
96
115
|
return next();
|
|
97
116
|
const method = domain[name];
|
|
98
117
|
try {
|