Improve websocket types

This commit is contained in:
Andrew Kingston 2022-08-23 08:48:16 +01:00
parent 8148004d55
commit c192ac534c
2 changed files with 7 additions and 10 deletions

View File

@ -14,7 +14,7 @@ export const initWebsocket = () => {
const tls = location.protocol === "https:" const tls = location.protocol === "https:"
const proto = tls ? "wss:" : "ws:" const proto = tls ? "wss:" : "ws:"
const host = location.hostname const host = location.hostname
const port = location.port || (tls ? 433 : 80) const port = location.port || (tls ? 443 : 80)
console.log(`${proto}//${host}:${port}`) console.log(`${proto}//${host}:${port}`)
const socket = io(`${proto}//${host}:${port}`, { const socket = io(`${proto}//${host}:${port}`, {
path: "/socket/client", path: "/socket/client",

View File

@ -1,20 +1,17 @@
import SocketIO from "socket.io" import { Server } from "socket.io"
import http from "http"
export class Websocket { export class Websocket {
socketIO: any socketServer: Server
constructor(server: any, path: string) { constructor(server: http.Server, path: string) {
// @ts-ignore this.socketServer = new Server(server, {
this.socketIO = SocketIO(server, {
path, path,
cors: {
origin: "*",
},
}) })
} }
// Emit an event to all sockets // Emit an event to all sockets
emit(event: string, payload: any) { emit(event: string, payload: any) {
this.socketIO.sockets.emit(event, payload) this.socketServer.sockets.emit(event, payload)
} }
} }