1 | const Koa = require("koa");
|
2 | const path = require("path");
|
3 | const EventEmitter = require("events");
|
4 |
|
5 | const route = require("koa-route");
|
6 | const views = require("koa-views");
|
7 | const websocket = require("koa-websocket");
|
8 |
|
9 | const parseSocket = require("./parse/socket");
|
10 | const cors = require('./../util/cors')
|
11 |
|
12 | class HttpEmitter extends EventEmitter {}
|
13 |
|
14 | var socketList = {};
|
15 | let socketMap;
|
16 |
|
17 | function emitterSocketEvent(route, message) {
|
18 | if (socketList[route]) {
|
19 | let routeList = socketList[route];
|
20 | routeList.map(item => {
|
21 | item.httpEmitter.emit("sendMessage", JSON.stringify(message));
|
22 | });
|
23 | }
|
24 | }
|
25 |
|
26 | function emitterSocketClose(route) {
|
27 | if (socketList[route]) {
|
28 | let routeList = socketList[route];
|
29 | routeList.map(item => {
|
30 | item.httpEmitter.emit("close", route);
|
31 | });
|
32 | delete socketList[route];
|
33 | }
|
34 | }
|
35 |
|
36 | function socketServer(socketConfig) {
|
37 | const port = socketConfig.port;
|
38 | const host = socketConfig.host;
|
39 | const directory = socketConfig.directory;
|
40 | const app = websocket(new Koa());
|
41 |
|
42 | if (directory) {
|
43 | socketMap = parseSocket(directory);
|
44 | }
|
45 |
|
46 | app.on("error", err => {
|
47 | console.log("出现了错误 ", err);
|
48 | });
|
49 |
|
50 | app.use(
|
51 | views(path.resolve(__dirname), {
|
52 | extension: "html"
|
53 | })
|
54 | );
|
55 |
|
56 | app.use(cors)
|
57 |
|
58 | app.use(
|
59 | route.get("/", async ctx => {
|
60 | await ctx.render("index", {
|
61 | socketList
|
62 | });
|
63 | })
|
64 | );
|
65 |
|
66 | app.use(
|
67 | route.get("/mock/sendMessage", async ctx => {
|
68 |
|
69 | emitterSocketEvent(ctx.query.url, ctx.query.message);
|
70 | ctx.response.status = 200;
|
71 | ctx.response.body = {
|
72 | code: 0
|
73 | };
|
74 | })
|
75 | );
|
76 |
|
77 | app.use(
|
78 | route.get("/mock/closeSocket", async ctx => {
|
79 |
|
80 | emitterSocketClose(ctx.query.url);
|
81 | ctx.response.status = 200;
|
82 | ctx.response.body = {
|
83 | msg: "socket close"
|
84 | };
|
85 | })
|
86 | );
|
87 |
|
88 | app.use(
|
89 | route.get("/mock/refresh", async ctx => {
|
90 | ctx.response.status = 200;
|
91 | ctx.response.body = {
|
92 | socketList: socketList,
|
93 | port: port
|
94 | };
|
95 | })
|
96 | );
|
97 |
|
98 | app.ws.use(
|
99 | route.all("*", ctx => {
|
100 | let httpEmitter = new HttpEmitter();
|
101 |
|
102 | if (socketList[ctx.url]) {
|
103 | socketList[ctx.url].push({
|
104 | url: ctx.url,
|
105 | httpEmitter: httpEmitter
|
106 | });
|
107 | } else {
|
108 | socketList[ctx.url] = [
|
109 | {
|
110 | url: ctx.url,
|
111 | httpEmitter: httpEmitter
|
112 | }
|
113 | ];
|
114 | }
|
115 | httpEmitter.on("sendMessage", function(data) {
|
116 | console.log("🐸🐸🐸 服务器发送的数据 ", data);
|
117 | ctx.websocket.send(data);
|
118 | });
|
119 | ctx.websocket.on("message", async message => {
|
120 | console.log("🐸🐸🐸🐸 服务器收到的数据 ", message);
|
121 | if (socketMap && socketMap[ctx.url]) {
|
122 | let returnMsg = await socketMap[ctx.url](message);
|
123 | emitterSocketEvent(ctx.url, returnMsg);
|
124 | }
|
125 | });
|
126 | httpEmitter.on("close", function() {
|
127 | console.log("socket close");
|
128 | ctx.websocket.close();
|
129 | });
|
130 | })
|
131 | );
|
132 |
|
133 | app.listen(port, host);
|
134 |
|
135 | console.log(`socket server is running on: ${host}:${port}`);
|
136 | }
|
137 |
|
138 | module.exports = {
|
139 | socketServer,
|
140 | emitterSocketEvent
|
141 | };
|