import { EventEmitter } from "events" ; import * as http from "http" ; import { HttpServer } from "@fluidframework/server-services-shared" ; import { IWebServer , IWebServerFactory , IWebSocket , IWebSocketServer , RequestListener , } from "@fluidframework/server-services-core" ; import { Server , Socket } from "socket.io" ; import { WebServer } from "./webServer" ; class SocketIoSocket implements IWebSocket { public get id ( ) : string { return this . socket . id ; } constructor ( private readonly socket ) { } public on ( event , listener ) { this . socket . on ( event , listener ) ; } public async join ( id ) { return this . socket . join ( id ) ; } public async emit ( event , ... args ) { this . socket . emit ( event , ... args ) ; } public async emitToRoom ( roomId , event , ... args ) { this . socket . nsp . to ( roomId ) . emit ( event , ... args ) ; } public async broadcastToRoom ( roomId , event , ... args ) { this . socket . to ( roomId ) . emit ( event , ... args ) ; } public disconnect ( close ? ) { return ; } } class SocketIoServer extends EventEmitter implements IWebSocketServer { constructor ( server , private readonly io ) { super ( ) ; this . io . attach ( server ) ; this . io . on ( "connection" , ( socket ) => { const webSocket = new SocketIoSocket ( socket ) ; this . emit ( "connection" , webSocket ) ; } ) ; } public async close ( ) { await new Promise < void > ( ( resolve ) => this . io . close ( ( ) => resolve ( ) ) ) ; } } export class WebServerFactory implements IWebServerFactory { constructor ( private readonly io ) { } public create ( requestListener ) { const server = http . createServer ( requestListener ) ; const httpServer = new HttpServer ( server ) ; const socketIoServer = new SocketIoServer ( server , this . io ) ; return new WebServer ( httpServer , socketIoServer ) ; } }