如何从 Postman/WebSocket King 调用 SignalR Core 集线器方法 [英] How to invoke a SignalR Core hub method from Postman/WebSocket King

查看:37
本文介绍了如何从 Postman/WebSocket King 调用 SignalR Core 集线器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SignalR Core 集线器.我可以使用 Postman 或 WebSocket King 等客户端连接到同一个集线器.但是,我无法调用需要参数的集线器方法.可以从 SignalR JS 客户端轻松调用相同的方法.我检查了浏览器开发工具并提取了从客户端传输到服务器的消息.当我尝试使用相同的消息从 Postman/WebSocket King 调用相同的方法时,它就不起作用了.

I have a SignalR Core hub. I can connect to the same hub using clients like Postman or WebSocket King. But, I am unable to invoke a hub method that expects parameters. The same method can be easily invoked from SignalR JS client. I inspected browser developer tools and extracted the message getting transferred from client to server. When I try to invoke the same method from Postman/WebSocket King with the same message it just don't work.

集线器方法:

public async Task Subscribe(string[] imeis, string group)
{
   ...     
}

从 SignalR 客户端传输的消息(从浏览器开发工具访问):

Message getting transferred from SignalR client (accessed from browser developer tools):

{"arguments":[[10001001],"ALL"],"invocationId":"0","streamIds":[],"target":"Subscribe","type":1}

从 Postman/WebSocket King 发送相同的消息不会命中 hub 方法.有什么建议吗?

From Postman/WebSocket King, sending this same message does not hit the hub method. Any suggestions?

推荐答案

好吧,就我而言,我想我已经能够连接到服务器了.所以基本上,这是一个两步的过程:

Ok, so for my case, I think I've been able to connect to the server. So basically, it is a 2-step process:

1.建立连接

发出 HTTP POST 请求到:<SIGNALR_CORE_SERVER_HUB_URL>/negotiate?negotiateVersion=1

Issue an HTTP POST request to: <SIGNALR_CORE_SERVER_HUB_URL>/negotiate?negotiateVersion=1

响应应该是这样的:

HTTP/1.1 200 OK
Content-Length: 256
Content-Type: application/json
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: null
Access-Control-Allow-Credentials: true
X-Powered-By: ASP.NET
Date: Tue, 17 Oct 2021 09:08:59 GMT

{"negotiateVersion":1,"connectionId":"TP- 
KX7BpPj4y4rxMFwaznw","connectionToken":"OeGB- 
EKkoYvcSg7Z4p4n0w","availableTransports": 
[{"transport":"WebSockets","transferFormats":["Text","Binary"]}, 
{"transport":"LongPolling","transferFormats":["Text","Binary"]}]}

连接令牌值 (OeGB-EKkoYvcSg7Z4p4n0w) 在这里很重要!保存以在第 2 步中使用.

The connection token value (OeGB-EKkoYvcSg7Z4p4n0w) is important here! Save it for use in Step 2.

<强>2.调用 SignalR hub 方法:

使用 WebSocket 协议,调用 URL:<SIGNALR_CORE_SERVER_HUB_URL>?id=OeGB-EKkoYvcSg7Z4p4n0w

Using WebSocket protocol, invoke the URL:<SIGNALR_CORE_SERVER_HUB_URL>?id=OeGB-EKkoYvcSg7Z4p4n0w

连接建立成功后,通过websocket将如下数据传入服务器:

After successful connection establishment, pass in the following data to server through websocket:

{"protocol":"json","version":1}

这将导致从服务器获得空响应.在此之后,调用服务器集线器方法并按如下方式传递参数:

This will result in getting an empty response from server. Following this, invoke the server hub method and pass the arguments as follows:

{"arguments":[["10001001"],"ALL"],"invocationId":"0","streamIds": 
[],"target":"Subscribe","type":1}

在这里,订阅"是我的 SignalR Core Hub 方法名称,它接受两个参数,一个字符串数组([10001001"])和一个字符串(ALL").

Here, "Subscribe" is my SignalR Core Hub method name, which accepts two arguments, an array of strings (["10001001"]) and a string ("ALL").

在此之后,从服务器端,推送通知将开始通过相同的 websocket 连接到达客户端.

After this, from server side, push notifications will start coming to client through the same websocket connection.

这篇关于如何从 Postman/WebSocket King 调用 SignalR Core 集线器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆