From 92a5f2063c5b29d05febf09b7d7aff78705331d9 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Fri, 15 Nov 2019 23:46:54 -0300 Subject: [PATCH] Use SignalData on server-side This should not cause compile issues when simple-peer is not installed (because client-side JS will be precompiled after pkg is published in NPM repository) because types are not used in runtime. --- src/client/actions/PeerActions.ts | 4 ++-- src/server/app.ts | 1 + src/server/socket.test.ts | 5 +++-- src/shared/SocketEvent.ts | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/client/actions/PeerActions.ts b/src/client/actions/PeerActions.ts index 65dab1c..59ff633 100644 --- a/src/client/actions/PeerActions.ts +++ b/src/client/actions/PeerActions.ts @@ -2,7 +2,7 @@ import * as ChatActions from '../actions/ChatActions' import * as NotifyActions from '../actions/NotifyActions' import * as StreamActions from '../actions/StreamActions' import * as constants from '../constants' -import Peer from 'simple-peer' +import Peer, { SignalData } from 'simple-peer' import forEach from 'lodash/forEach' import _debug from 'debug' import { play, iceServers } from '../window' @@ -42,7 +42,7 @@ class PeerHandler { peer && peer.destroy() dispatch(removePeer(user.id)) } - handleSignal = (signal: unknown) => { + handleSignal = (signal: SignalData) => { const { socket, user } = this debug('peer: %s, signal: %o', user.id, signal) diff --git a/src/server/app.ts b/src/server/app.ts index f3cba76..7ba1f32 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -33,5 +33,6 @@ router.use('/', index) app.use(BASE_URL, router) io.on('connection', socket => handleSocket(socket, io)) +io.sockets.adapter.rooms['test'].sockets export default server diff --git a/src/server/socket.test.ts b/src/server/socket.test.ts index 78c7333..9736e74 100644 --- a/src/server/socket.test.ts +++ b/src/server/socket.test.ts @@ -1,6 +1,7 @@ import { EventEmitter } from 'events' +import { Socket } from 'socket.io' +import { TypedIO } from '../shared' import handleSocket from './socket' -import { Socket, Server } from 'socket.io' describe('server/socket', () => { type SocketMock = Socket & { @@ -12,7 +13,7 @@ describe('server/socket', () => { } let socket: SocketMock - let io: Server & { + let io: TypedIO & { in: jest.Mock<(room: string) => SocketMock> to: jest.Mock<(room: string) => SocketMock> } diff --git a/src/shared/SocketEvent.ts b/src/shared/SocketEvent.ts index 9e7b5ce..a0ae5bb 100644 --- a/src/shared/SocketEvent.ts +++ b/src/shared/SocketEvent.ts @@ -1,4 +1,5 @@ import { TypedEmitter, TypedEmitterKeys } from './TypedEmitter' +import { SignalData } from 'simple-peer' export interface User { id: string @@ -12,7 +13,7 @@ export interface SocketEvent { signal: { userId: string // eslint-disable-next-line - signal: any + signal: SignalData } connect: undefined disconnect: undefined