SignalR、websockets 和 go
SignalR, websockets and go
我需要从 Go 连接到 SignalR 服务器。我一直在尝试使用一些库和文章来帮助我:
- https://github.com/x2v3/signalr
- https://github.com/devigned/signalr-go
- https://www.derpturkey.com/signalr-is-an-abomination-how-to-connect-using-raw-websockets/
- https://blog.3d-logic.com/2015/03/29/signalr-on-the-wire-an-informal-description-of-the-signalr-protocol/
问题是我需要从不同的端口检索令牌。我的 SignalR 服务器地址是 https://server:10446/endpoint
,但我需要从 https://server:10443/connect/token
检索我的令牌,这偏离了正常的 SignalR 协议。
我可以拿到我的令牌了。并能够让协商步骤得到以下响应:
{
"connectionId": "JkOfCC0JykJ8hfzm8jd53w",
"availableTransports": [
{
"transport": "WebSockets",
"transferFormats": [
"Text",
"Binary"
]
},
{
"transport": "ServerSentEvents",
"transferFormats": [
"Text"
]
},
{
"transport": "LongPolling",
"transferFormats": [
"Text",
"Binary"
]
}
]
}
然后我尝试通过使用大猩猩 websockets 进行 websocket 连接来获得连接:
wss://server:10446/endpoint?transport=WebSockets&id=${ConnectionIDFromNegotiate}&connectionData=[{\"name\":\"HubName\"}]&access_token=${MyRetrievedToken}
在这一点上,我收到一个回复说 101
,已升级到 websocket。但是,从这一刻起,我无法继续握手。我正在向服务器发送消息,但除了 15-20 秒后握手中止外,我没有收到任何响应。
这是我用来连接和发送握手消息的代码:
ws, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}
if err = ws.WriteMessage(websocket.TextMessage, []byte(`{"protocol": "json","version": 1}`)); err != nil {
return nil, err
}
任何想法都将受到热烈欢迎:)
Brennan 给出了解决方案。
非常感谢!
It looks like you're missing part of the message payload. Spec github.com/dotnet/aspnetcore/blob/master/src/SignalR/docs/specs/… "Both the HandshakeRequest and HandshakeResponse messages must be terminated by the ASCII character 0x1E (record separator)."
我需要从 Go 连接到 SignalR 服务器。我一直在尝试使用一些库和文章来帮助我:
- https://github.com/x2v3/signalr
- https://github.com/devigned/signalr-go
- https://www.derpturkey.com/signalr-is-an-abomination-how-to-connect-using-raw-websockets/
- https://blog.3d-logic.com/2015/03/29/signalr-on-the-wire-an-informal-description-of-the-signalr-protocol/
问题是我需要从不同的端口检索令牌。我的 SignalR 服务器地址是 https://server:10446/endpoint
,但我需要从 https://server:10443/connect/token
检索我的令牌,这偏离了正常的 SignalR 协议。
我可以拿到我的令牌了。并能够让协商步骤得到以下响应:
{
"connectionId": "JkOfCC0JykJ8hfzm8jd53w",
"availableTransports": [
{
"transport": "WebSockets",
"transferFormats": [
"Text",
"Binary"
]
},
{
"transport": "ServerSentEvents",
"transferFormats": [
"Text"
]
},
{
"transport": "LongPolling",
"transferFormats": [
"Text",
"Binary"
]
}
]
}
然后我尝试通过使用大猩猩 websockets 进行 websocket 连接来获得连接:
wss://server:10446/endpoint?transport=WebSockets&id=${ConnectionIDFromNegotiate}&connectionData=[{\"name\":\"HubName\"}]&access_token=${MyRetrievedToken}
在这一点上,我收到一个回复说 101
,已升级到 websocket。但是,从这一刻起,我无法继续握手。我正在向服务器发送消息,但除了 15-20 秒后握手中止外,我没有收到任何响应。
这是我用来连接和发送握手消息的代码:
ws, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return nil, err
}
if err = ws.WriteMessage(websocket.TextMessage, []byte(`{"protocol": "json","version": 1}`)); err != nil {
return nil, err
}
任何想法都将受到热烈欢迎:)
Brennan 给出了解决方案。 非常感谢!
It looks like you're missing part of the message payload. Spec github.com/dotnet/aspnetcore/blob/master/src/SignalR/docs/specs/… "Both the HandshakeRequest and HandshakeResponse messages must be terminated by the ASCII character 0x1E (record separator)."