chore: add full source code
This commit is contained in:
40
src/implements/socket.io/SocketClient.ts
Normal file
40
src/implements/socket.io/SocketClient.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { SocketClient as SocketClientBase } from "@/core/SocketClient";
|
||||
import { io } from "socket.io-client";
|
||||
import { SocketConnection } from "./SocketConnection";
|
||||
|
||||
export class SocketClient implements SocketClientBase {
|
||||
|
||||
public async connect(url: string): Promise<SocketConnection> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = io(url, {
|
||||
autoConnect: false,
|
||||
reconnection: false,
|
||||
});
|
||||
|
||||
const conn = new SocketConnection({
|
||||
sendMethod: (data) => {
|
||||
socket.emit('c', data);
|
||||
},
|
||||
closeMethod: () => {
|
||||
socket.close();
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('connect', () => {
|
||||
resolve(conn);
|
||||
});
|
||||
|
||||
socket.on('disconnect', (reason, description) => {
|
||||
conn.emit('closed', reason);
|
||||
})
|
||||
|
||||
/** subscribe messages from server */
|
||||
socket.on('s', (data) => {
|
||||
conn.emit('msg', data);
|
||||
})
|
||||
|
||||
socket.connect();
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
25
src/implements/socket.io/SocketConnection.ts
Normal file
25
src/implements/socket.io/SocketConnection.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { SocketConnection as SocketConnectionBase, SocketConnectionBaseEvents } from "@/core/SocketConnection";
|
||||
import { EventEmitter } from "@/utils/EventEmitter";
|
||||
|
||||
interface SocketConnectionEvents extends SocketConnectionBaseEvents {
|
||||
|
||||
};
|
||||
|
||||
export class SocketConnection
|
||||
extends EventEmitter<SocketConnectionBaseEvents> implements SocketConnectionBase {
|
||||
|
||||
constructor(private args: {
|
||||
sendMethod: (data: any) => void;
|
||||
closeMethod: () => void;
|
||||
}) {
|
||||
super();
|
||||
}
|
||||
|
||||
public async send(data: any): Promise<void> {
|
||||
this.args.sendMethod(data);
|
||||
}
|
||||
|
||||
public async close(): Promise<void> {
|
||||
this.args.closeMethod();
|
||||
}
|
||||
}
|
||||
42
src/implements/socket.io/SocketServer.ts
Normal file
42
src/implements/socket.io/SocketServer.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { SocketServer as SocketServerBase, SocketServerBaseEvents } from "@/core/SocketServer";
|
||||
import { EventEmitter } from "@/utils/EventEmitter";
|
||||
import { SocketConnection } from "./SocketConnection";
|
||||
|
||||
interface SocketServerEvents extends SocketServerBaseEvents {
|
||||
|
||||
}
|
||||
|
||||
export class SocketServer extends EventEmitter<SocketServerEvents> implements SocketServerBase {
|
||||
|
||||
public async listen(options: { port: number; }): Promise<void> {
|
||||
const { port } = options;
|
||||
/** only run it */
|
||||
const { Server } = await import("socket.io");
|
||||
const io = new Server();
|
||||
|
||||
io.on('connection', socket => {
|
||||
const conn = new SocketConnection({
|
||||
sendMethod: (data) => {
|
||||
socket.emit('s', data);
|
||||
},
|
||||
closeMethod: () => {
|
||||
socket.conn.close();
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('disconnect', (reason, description) => {
|
||||
conn.emit('closed', reason);
|
||||
})
|
||||
|
||||
/** subscribe messages from client */
|
||||
socket.on('c', (data) => {
|
||||
conn.emit('msg', data);
|
||||
})
|
||||
|
||||
this.emit('connect', conn);
|
||||
})
|
||||
|
||||
io.listen(port);
|
||||
}
|
||||
|
||||
}
|
||||
9
src/implements/socket.io/index.ts
Normal file
9
src/implements/socket.io/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { injectSocketClient } from "@/core/SocketClient";
|
||||
import { SocketClient } from "./SocketClient";
|
||||
import { injectSocketServer } from "@/core/SocketServer";
|
||||
import { SocketServer } from "./SocketServer";
|
||||
|
||||
export function injectSocketIOImplements() {
|
||||
injectSocketClient(SocketClient);
|
||||
injectSocketServer(SocketServer);
|
||||
}
|
||||
Reference in New Issue
Block a user