in sdk/webpubsub-socketio-extension/src/SIO/index.ts [24:78]
export async function useAzureSocketIOChain(
this: SIO.Server,
webPubSubOptions: AzureSocketIOOptions | AzureSocketIOCredentialOptions
): Promise<SIO.Server> {
debug(`useAzureSocketIOChain, webPubSubOptions: ${JSON.stringify(webPubSubOptions)}`);
const engine = new WebPubSubEioServer(this.engine.opts, webPubSubOptions);
const httpServer = this["httpServer"];
engine.attach(httpServer, this["opts"]);
// Add negotiate handler
debug("add negotiate handler");
const path = this._opts.path || DEFAULT_SIO_PATH;
// current listeners = EIO handleRequest listeners (e.g. /socket.io) + other listeners from user
const listeners = httpServer.listeners("request").slice(0);
httpServer.removeAllListeners("request");
this[WEB_PUBSUB_OPTIONS_PROPERY_NAME] = webPubSubOptions;
httpServer.on("request", (req: IncomingMessage, res: ServerResponse) => {
// Requests routing to EIO `handleRequest` listener should be forbidden. Other listeners should be handled as normal.
for (let i = 0; i < listeners.length; i++) {
// Follow the same logic as Engine.IO
// Reference: https://github.com/socketio/engine.io/blob/6.4.2/lib/server.ts#L804
if (path !== req.url.slice(0, path.length)) {
listeners[i].call(httpServer, req, res);
} else {
debug(`Forbidden request whose url ${req.url} starts with ${path}`);
writeResponse(res, 403, FORBIDDEN_REQUEST_MESSAGE, "text/plain");
}
}
});
// `attachServe` is a Socket.IO design which attachs static file serving to internal http server.
// Creating new engine makes previous `attachServe` execution invalid.
// Reference: https://github.com/socketio/socket.io/blob/4.6.2/lib/index.ts#L518
debug("serve static file");
if (this["_serveClient"]) {
this["attachServe"](httpServer);
}
this.bind(engine);
debug("use webPubSub adatper");
const adapterProxy = new WebPubSubAdapterProxy(
(this.engine as WebPubSubEioServer).webPubSubConnectionManager.service
);
this.adapter(adapterProxy as unknown as AdapterConstructor);
this.use(getSioMiddlewareFromExpress(restoreClaims()));
// If using tunnel, wait until connected. `engine.setup` does no nothing when using REST API.
await engine.setup();
return this;
}