Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

WebAdapter: add getConnection method #1666

Merged
merged 2 commits into from
May 20, 2019

Conversation

Naktibalda
Copy link
Contributor

Direct access to WebSocket connection can be useful, but there were no means to access connection of specific user.

What I actually wanted to do:

I got a feature request to log user's ip and user agent, but they are only available in on connection event handler as a second parameter, so I implemented event handler and made it assign custom properties to WebSocket objects.

if (controller.adapter instanceof WebAdapter) {
    controller.ready(() => {
        const wsServer: WebSocket.Server = controller.adapter.wss;
        wsServer.on('connection', (ws, req) => {
            ws.ip = req.connection.remoteAddress;
            ws.userAgent = req.headers['user-agent'];
        });
    });
}

Then I can access them in my bot's code like this:

if (bot.controller.adapter instanceof WebAdapter) {
	const connection = bot.controller.adapter.getConnection(message.user);
	console.log(connection.ip);
	console.log(connection.userAgent);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants