入门19_goWebSocket

go websocket安全校验机制 jwt

01,属于get,put还是post

get,在线测试工具没选项,查看报文实际用的是get

02,如何传递参数?

url参数:类似http的get请求,url中可以携带参数,后端获取方法和http一样的。
header参数:python

1
2
3
ws=websocket.WebSocket()
#建立websocket连接,这里传入了 header ,需要注意header的写入方式
ws.connect("ws://xx.xx.xx.xx 8000/port/streaming?vend=Test", header=["x-token:xxxxxxxxxxxxxx", "x-tenant:T0014", "x-server:1026"])

post能取得body么?好像不能post连接。

03,接口测试方法

a,postman支持。
b.在线工具,百度:websocket在线测试,很多工具。
c,curl,但curl的支持方式比较奇怪

1
2
3
4
5
6
7
8
9
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: echo.websocket.org" \
--header "Origin: https://echo.websocket.org" \
--header "Sec-WebSocket-Key: NVwjmQUcWCenfWu98asDmg==" \
--header "Sec-WebSocket-Version: 13" \
http://echo.websocket.org

其中:
01,地址填写,http://echo.websocket.org,而非 ws://echo.websocket.org
02,注意这里不需要特定的密钥(Sec-WebSocket-Key),所以随便取一个都可以。该头文件的作用是防止缓存websocket请求。

不过,联通后,有输入光标的,但却无法向server发送出消息(不确定是否自己方法有问题)。只能做是否联通的检查,
d,websocat,websocket调通后,可取代curl,可交互输入消息
参考:https://github.com/vi/websocat

wesocat报错不完善:

1
2
3
4
命令:websocat ws://127.0.0.1:8002/ws?name=tx3
响应:
websocat: WebSocketError: Received unexpected status code (200 OK)
websocat: error running

而curl响应:

1
2
3
4
5
6
7
HTTP/1.1 200 OK
Content-Type: application/json
Server: gf-demos
Date: Thu, 28 Apr 2022 07:57:41 GMT
Content-Length: 44

{"code":1,"message":"cookie token is empty"}

包含完整报错信息。

websocat可添加header字段支持(jwt)

1
websocat --header="Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTE3Mzg5NjUsImlkIjoxLCJvcmlnX2lhdCI6MTY1MTEzNDE2NSwicGFzc3BvcnQiOiJlc3dpbjAxIn0.ZawjnuSNso9vK5NXF1nOmCVPpWBICX83rmuZvz1LQ7o"  "ws://127.0.0.1:8002/ws"

04,集成jwt

其实就是header添加token,并且保证中间件可以正确识别解析

#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×