BlogArticles/2024-09/ApiFox _ Postman 使用WebSocke...

60 lines
2.7 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
{
title: "ApiFox / Postman 使用WebSocket连接SignalR需要注意的小问题",
description: "使用ApiFox测试SignalR时发现消息发送无响应原因为未在消息末尾添加结束符0x1e。正确格式为消息体后加0x1e字符如`{\"protocol\":\"json\",\"version\":1}`。调用方法需按指定JSON结构并注意不同类型消息的含义。",
draft: false,
type: "article",
created_at: "2024-09-11T15:00:00+08:00",
published_at: "2024-09-11T15:08:00+08:00",
updated_at: [ "2024-09-11T15:08:00+08:00"],
category: '个人',
tags: [ "WebSocket" ],
tech_stack: [ "WebSocket" ],
tech_stack_percent: [ 1 ],
tech_stack_icon_names: [ "simple-icons:socketdotio" ],
tech_stack_theme_colors: [ "#fdfdfd" ],
}
---
!!!warning Legacy Article 过时的文章
此文章从旧博客迁移而来,编写时技术水平有限,仅供参考
!!!
省流忘加0x1e
最近刚接触SignalR发现用ApiFox怎么都没法测试可以正常连接但是发消息没有任何反应。我一度怀疑是自己的环境出了问题。
![image.png](https://typecho.lichx.top/usr/uploads/2024/09/1251616581.png)
后来直接使用大佬的代码带前端进行测试发现是正常的。说明这只是操作上的问题。又去搜postman调试SignalR。才找到了这个简单的隐蔽的问题。
SignalR的交流以0x1e作为结束符。之前没有注意到这一点走了一些弯路。
`{"protocol":"json","version":1}`
这段代码末尾加了个0x1e。可以直接整段复制用于SignalR连接。
![image.png](https://typecho.lichx.top/usr/uploads/2024/09/2409001684.png)
TakeAway message:
如果想要上传消息,格式是:
{
"type": 1,
"target": "方法名",
"arguments": ["方法参数1", "方法参数2..."]
}
SignalR type 数字含义From Copilot
1. Invocation (1)
• 表示客户端或服务器调用一个方法。
• 例如客户端调用服务器上的一个Hub方法。
2. StreamItem (2)
• 表示流中的一项数据。
• 用于流式传输数据时,每个数据项都会使用这个消息类型。
3. Completion (3)
• 表示一个调用或流的完成。
• 包含调用的结果或错误信息。
4. StreamInvocation (4)
• 表示客户端请求从服务器流式传输数据。
• 服务器会返回多个StreamItem消息。
5. CancelInvocation (5)
• 表示取消一个流式传输的请求。
• 客户端可以发送这个消息来取消一个正在进行的流。
6. Ping (6)
• 表示一个心跳消息,用于保持连接活跃。
• 没有负载,只是为了确保连接没有超时。
7. Close (7)
• 表示连接关闭。
• 包含可选的错误信息。