Add TypedSocket to server
This commit is contained in:
parent
45b5a3bbf2
commit
703c13f296
@ -1,13 +1,11 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
import _debug from 'debug'
|
import _debug from 'debug'
|
||||||
import map from 'lodash/map'
|
import map from 'lodash/map'
|
||||||
import { Socket, Server } from 'socket.io'
|
import { ServerSocket, TypedIO } from '../shared'
|
||||||
|
|
||||||
const debug = _debug('peercalls:socket')
|
const debug = _debug('peercalls:socket')
|
||||||
|
|
||||||
type SocketWithRoom = Socket & { room?: string }
|
export default function handleSocket(socket: ServerSocket, io: TypedIO) {
|
||||||
|
|
||||||
export default function handleSocket(socket: SocketWithRoom, io: Server) {
|
|
||||||
socket.on('signal', payload => {
|
socket.on('signal', payload => {
|
||||||
// debug('signal: %s, payload: %o', socket.id, payload)
|
// debug('signal: %s, payload: %o', socket.id, payload)
|
||||||
io.to(payload.userId).emit('signal', {
|
io.to(payload.userId).emit('signal', {
|
||||||
|
|||||||
28
src/shared/SocketEvent.ts
Normal file
28
src/shared/SocketEvent.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { TypedEmitter, TypedEmitterKeys } from './TypedEmitter'
|
||||||
|
|
||||||
|
export interface User {
|
||||||
|
id: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SocketEvent {
|
||||||
|
users: {
|
||||||
|
initiator: string
|
||||||
|
users: User[]
|
||||||
|
}
|
||||||
|
signal: {
|
||||||
|
userId: string
|
||||||
|
signal: unknown
|
||||||
|
}
|
||||||
|
connect: void
|
||||||
|
disconnect: void
|
||||||
|
ready: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ServerSocket =
|
||||||
|
Omit<SocketIO.Socket, TypedEmitterKeys> &
|
||||||
|
TypedEmitter<SocketEvent> &
|
||||||
|
{ room?: string }
|
||||||
|
|
||||||
|
export type TypedIO = SocketIO.Server & {
|
||||||
|
to(roomName: string): TypedEmitter<SocketEvent>
|
||||||
|
}
|
||||||
@ -5,6 +5,14 @@ type Callback<A> = (a: A) => void
|
|||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
type Events = Record<string | symbol, any>
|
type Events = Record<string | symbol, any>
|
||||||
|
|
||||||
|
export type TypedEmitterKeys =
|
||||||
|
'addListener' |
|
||||||
|
'removeListener' |
|
||||||
|
'on' |
|
||||||
|
'once' |
|
||||||
|
'off' |
|
||||||
|
'emit'
|
||||||
|
|
||||||
export interface TypedEmitter<E extends Events>
|
export interface TypedEmitter<E extends Events>
|
||||||
extends EventEmitter {
|
extends EventEmitter {
|
||||||
addListener<K extends keyof E>(t: K, callback: Callback<E[K]>): this
|
addListener<K extends keyof E>(t: K, callback: Callback<E[K]>): this
|
||||||
|
|||||||
2
src/shared/index.ts
Normal file
2
src/shared/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from './SocketEvent'
|
||||||
|
export * from './TypedEmitter'
|
||||||
Loading…
x
Reference in New Issue
Block a user