如何将Python Websocket客户端连接到我的弹簧端点?
我是Python的新手,我正在尝试连接到目前在春季运行的服务。
在服务器端,我通过扩展DefaulthandShakeHandler并覆盖确定使用者方法,在该方法中使用自定义的握手处理程序,然后在此提取用户信息。但是,当请求进来时,没有什么可提取的。
我想知道的是,如何通过请求发送此信息?
这是我的基本Python客户端。
import websocket
import stomper
headers = {}
#### have also tried with headers = Authorization : Bearer + token ####
uri = "ws://localhost:[port]/ws"
def on_msg(ws,msg):
print(msg)
def on_error(ws,err):
print(err)
def on_closed(ws):
print(#Closed#)
def on_open(ws):
ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\nx00n")
sub = stomper.subscribe("/user/topic/my-end-point", "username", ack="auto")
ws.send(sub)
ws = websocket.WebSocketApp(uri, header=headers, on_message=on_msg, on_error=on_error, on_close=on_closed)
ws.on_open = on_open
ws.run.forever()
当我运行此功能时,我会得到以下内容:
\--- request header ---
GET /ws HTTP/1.1
Upgrade: websocket
Host: localhost:\[port\]
Origin: http://localhost\[port\]
Sec-WebSocket-Key: key
Sec-WebSocket-version: 13
Connection: Upgrade
\--- response header ---
HTTP/1.1 101 Switching Protocols
Date: date
Connection: Upgrade
Sec-WebSocket-Accept: key
Server: server-info
Upgrade: WebSocket
send: b'\\x81...
CONNECTED
version: 1.1
session: session_id
server: server-info
然后在60秒后止住。
接下来我可以尝试什么?
I'm new to Python and I'm trying to connect to a service that I currently have up running in Spring.
On the server-side I have a custom Handshake handler in place by extending DefaultHandshakeHandler and overriding the determineUser method, where I then extract the user information. However, when the request comes in, there is nothing to extract.
What I would like to know is, how do I send this information over with the request?
Here is my basic Python client.
import websocket
import stomper
headers = {}
#### have also tried with headers = Authorization : Bearer + token ####
uri = "ws://localhost:[port]/ws"
def on_msg(ws,msg):
print(msg)
def on_error(ws,err):
print(err)
def on_closed(ws):
print(#Closed#)
def on_open(ws):
ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\nx00n")
sub = stomper.subscribe("/user/topic/my-end-point", "username", ack="auto")
ws.send(sub)
ws = websocket.WebSocketApp(uri, header=headers, on_message=on_msg, on_error=on_error, on_close=on_closed)
ws.on_open = on_open
ws.run.forever()
When I run this, I get the following:
\--- request header ---
GET /ws HTTP/1.1
Upgrade: websocket
Host: localhost:\[port\]
Origin: http://localhost\[port\]
Sec-WebSocket-Key: key
Sec-WebSocket-version: 13
Connection: Upgrade
\--- response header ---
HTTP/1.1 101 Switching Protocols
Date: date
Connection: Upgrade
Sec-WebSocket-Accept: key
Server: server-info
Upgrade: WebSocket
send: b'\\x81...
CONNECTED
version: 1.1
session: session_id
server: server-info
It then times out after 60 seconds.
What can I try next?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也许应该更新这个。
我在 url 字符串中缺少身份验证令牌参数。
现在它按预期工作了。
I should probably update this.
I was missing the authentication token parameter in the url string.
It works as expected now.