如何将客户端的Python套接字连接到Node.js/socket.io?
我想通过套接字将Blender(v2.55)连接到网页.
I want to connect Blender (v2.55) to a webpage through sockets.
对于Web部件,我可以使用Node.js&套接字我已经使用了一点node.js/socket.io,我认为这不是问题.
For the web part, I can use Node.js & socket.io. I've already used a little node.js/socket.io, it's not a problem I think.
现在用于Blender,它可以在Python 3.1上运行,因此我已经安装了套接字,并且可以根据需要添加库.我是Python套接字的新手,我可以直接将客户端连接到node.js/socket.io吗?
Now for Blender, it runs on Python 3.1, so I've already sockets and I can add libraries if needed. I'm new to Python sockets, can I connect a client to node.js/socket.io directly ?
我尝试了Python文档中的基本代码:
I tried with the basic code from the Python doc:
import socket
import sys
HOST, PORT = "127.0.0.1", 8080
data = "Hello from Blender"
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server and send data
sock.connect((HOST, PORT))
sock.send(bytes(data + "\n","utf8"))
# Receive data from the server and shut down
received = sock.recv(1024)
sock.close()
print("Sent: %s" % data)
print("Received: %s" % received)
其结果是:
Sent: Hello from Blender
Received: b''
似乎Blender已连接,但未接收数据.节点还显示没有新的客户端连接…
It seems that Blender is connected, but doesn't receive data. Also Node shows no new client connected…
我还需要其他东西吗?如果有人可以帮助我...
Do I need something else ? If somebody can help me out…
您缺少协议/握手.您所拥有的只是一个裸露的TCP套接字连接. node.js/socket.io位于TCP套接字的顶部.基本上,当您打开与socket.io服务器的连接时,期望您使用某种协议进行通信(websockets,longpolling,htmlfile等).初始握手定义该协议将是什么. Websockets是受支持的协议之一. 此博客文章应该会为您提供帮助.实现websocket看起来并不困难.
You are missing a protocol/handshake. What you have there is a bare TCP socket connection. node.js/socket.io lives on top of a TCP socket. Basically when you open a connection to a socket.io server, it's expecting you to use some protocol for communication (websockets, longpolling, htmlfile, whatever). The initial handshake defines what that protocol will be. Websockets is one of the supported protocols. This blog post should help you. It doesn't look all that hard to get websockets implemented.