简体   繁体   English

使用 ws WebSocket 进行身份验证

[英]Authenticating using ws WebSocket

I'm using ws webSocket (can't use socket.io) and I'm a bit new to it.我正在使用ws webSocket (不能使用 socket.io),我对它有点陌生 I know how it works, but don't fully get a couple of things.我知道它是如何工作的,但没有完全了解一些事情。

  1. How can I authenticate using jwt?如何使用 jwt 进行身份验证? The docs say that using the upgrade event is the correct way to go about it, but when the client connected, the code in the upgrade function didn't run.文档说使用upgrade事件是正确的方法,但是当客户端连接时, upgrade函数中的代码没有运行。

  2. What's the correct way to add an event type?添加事件类型的正确方法是什么? In socket.io, one can listen using client.on('eventType', function) .在 socket.io 中,可以使用client.on('eventType', function)进行client.on('eventType', function) What's the webSocket ws equivalent?什么是 webSocket ws 等价物?

  3. When and why would one use paths in webSocket ws?何时以及为什么会在 webSocket ws 中使用路径?

Update更新

The update event doesn't get fired on my end.更新事件不会在我结束时被触发。 Here's the code I have:这是我的代码:

File 1文件 1

const server = require('http').createServer(app);
require('./socket/socket')(server);

File 2档案 2

module.exports = function(server) {
    const WebSocket = require('ws');

    const wss = new WebSocket.Server({ port: 8080 });

    server.on('upgrade', function(request, socket, head) {
        console.log('Upgraded');

        wss.handleUpgrade(request, socket, head, function(ws) {
            wss.emit('connection', ws, request);
        });
    });

    wss.on('connection', function connection(ws) {
        ws.on('message', function incoming(message) {
            console.log('received: %s', message);
        });
    });
};
  1. ws provide auth examples . ws 提供身份验证示例 These work when a HTTP server first handle the auth requests.这些在 HTTP 服务器第一次处理身份验证请求时起作用。 Then pass a HTTP upgrade request to ws rather than ws itself listening on a port.然后将 HTTP 升级请求传递给 ws 而不是 ws 本身监听端口。 Note the noServer: true option they include in the setup:请注意它们包含在设置中的noServer: true选项:

     const wss = new WebSocket.Server({ clientTracking: false, noServer: true });

    The jwt component will be easier using koa or express in HTTP first then doing the upgrade in this way. jwt 组件首先使用koa或 HTTP 中的express会更容易,然后以这种方式进行升级。 Otherwise you would need to write a ws message handler to look for some token data and verify it.否则,您将需要编写一个 ws message处理程序来查找一些令牌数据并对其进行验证。

  2. The message is the event in ws. message是 ws 中事件。 You get to write anything more specific on top of the message event, which is a big reason to use socket.io that has done all that for you (including client callback functions, which are super helpful).您可以在message事件之上编写任何更具体的内容,这是使用 socket.io 的一个重要原因,它为您完成了所有工作(包括客户端回调函数,它们非常有用)。

  3. Using a URI path in the ws:// or http upgrade request would usually be to connect to a separate ws context on the server side.ws://或 http 升级请求中使用 URI 路径通常会连接到服务器端的单独ws上下文。 A simple way to namespace or separate connection into discreet groups.一种将命名空间或连接分离成谨慎组的简单方法。

How can I authenticate using jwt?如何使用 jwt 进行身份验证? The docs say that using the upgrade event is the correct way to go about it, but when the client connected, the code in the upgrade function didn't run.文档说使用升级事件是正确的方法,但是当客户端连接时,升级函数中的代码没有运行。

Well, there are lots of different choices.嗯,有很多不同的选择。 You could pass a token as a query parameter of a custom header when first making the connection.首次建立连接时,您可以将令牌作为自定义标头的查询参数传递。 You could require that the first data sent over the webSocket is a token and disconnect if such a token does not arrive soon and first.您可以要求通过 webSocket 发送的第一个数据是一个令牌,如果这样的令牌没有尽快到达,则断开连接。 If the client was already authenticated and there's an http cookie that indicates that, you could examine that cookie upon websocket connection.如果客户端已经过身份验证并且有一个 http cookie 表明这一点,您可以在 websocket 连接时检查该 cookie。

What's the correct way to add an event type?添加事件类型的正确方法是什么? In socket.io, one can listen using client.on('eventType', function).在 socket.io 中,可以使用 client.on('eventType', function) 进行侦听。 What's the webSocket ws equivalent?什么是 webSocket ws 等价物?

The socket.io message types (or event types as you call them) are something that socket.io adds on top of webSocket. socket.io 消息类型(或称为事件类型)是 socket.io 添加在 webSocket 之上的东西。 The webSocket protocol itself does not have such a thing. webSocket 协议本身没有这样的东西。 You send a data packet and that data packet arrives at the other end.您发送一个数据包,该数据包到达另一端。 All data packets are of the same webSocket "type".所有数据包都具有相同的 webSocket“类型”。 If you want to identify your data packets as being a certain type of message, then you have to invent a way of doing that inside your data back.如果您想将您的数据包识别为某种类型的消息,那么您必须发明一种在您的数据内部执行此操作的方法。 For example, if your data was JSON formatted, you could add a type: "someType" property to event message and then examine that property upon receipt (this is essentially what socket.io does although it does it outside of the socket.io payload).例如,如果您的数据是 JSON 格式,您可以向事件消息添加一个type: "someType"属性,然后在收到时检查该属性(这实际上是 socket.io 所做的,尽管它在 socket.io 负载之外执行)。

When and why would one use paths in webSocket ws?何时以及为什么会在 webSocket ws 中使用路径?

You may have two completely separate parts of code that use a webSocket for different things that have nothing in common and reside in separate modules and it's just more straightforward for each to have its own webSocket connection.您可能有两个完全独立的代码部分,它们将 webSocket 用于不同的事物,这些事物没有共同点并驻留在单独的模块中,并且每个部分都有自己的 webSocket 连接更简单。 And, they may be used by two completely separate types of clients.而且,它们可能被两种完全不同类型的客户端使用。

How to access the orignal URL when a webSocket client connects to your server is shown here on your server's upgrade event used with the ws library.当 webSocket 客户端连接到您的服务器时如何访问原始 URL 显示ws库一起使用的服务器upgrade事件中。

You will note in that example that the upgrade event occurs on the http server, not on the websocket server instance.您会在该示例中注意到upgrade事件发生在 http 服务器上,而不是 websocket 服务器实例上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM