I want to start a websocket server by running this script in Python Script.
Rhino7 crash when the code runs to the last line
I can’t find any error log
import clr
import System
from System.Threading import Thread, ThreadStart
from WebSocketSharp.Server import WebSocketServer, WebSocketBehavior
class EchoService(WebSocketBehavior):
def OnMessage(self, e):
self.Send("Echo: " + e.Data)
def start_server():
port = 4649
server = WebSocketServer(port)
server.AddWebSocketServiceEchoService
server.Start()
print(“WebSocket server started at ws://localhost:{}/Echo”.format(port))
while server.IsListening:
System.Threading.Thread.Sleep(1000)
Rhino crashes because you run code on a dedicated thread within the Rhino process. You need to try-catch/except whatever you do in this thread, otherwise you will just hard-crash the application on errors. Only on the main(=ui) thread you have this extra guard from Rhino and the script executor.
This is not a Websocket! Its a plain TCP socket. This is something different.
Also note that binding fails more often as you might think. Depending on the implementation it either it returns error codes or hard crashes the application on exception (or both). This code isn’t preventing anything from crashing Rhino.
I want to realize MCP in Rhino Refering Blender-MCP. Someone told me it’s a websocket; so I asked GPT how to start a websocket server in Rhino and followed its step.
Nevermind. Your solution is better than GPT’s
OK. I don’t know
I just attemptted to realize MCP in Rhino. Referring Blender-MCP on github. Someone told me it use a websocket but actually a TCP socket
I don’t know, but in here you see an implementation which uses a plain TCP server which seems to be quite resilient. It basically combines all the information you received here. But you are right, there is no web-socket involved
There are 3 things being mixed up a bit.
1.) Thread-safe coding. Its not a bug of Rhino if you cause un-handled exceptions. Always assume code can fail and on a different thread you need to handle it for yourself.
2.) Knowing and implementing the correct type of socket (A web-socket is also TCP based but has an additional layer. The web-socket header has a strict interface, and while it is technically possible to deal with web-sockets on plain sockets, it is still pointless to re-implement the wheel. A web-socket library might behave differently than the default socket implementation.
3.) Superficially knowing how sockets works, and what can go wrong.
All these topics require advanced coding knowledge. I personally just find it more useful to explain what can go wrong, instead of posting code snippets. So sorry if you expect this from me here, but don’t be demotivated if it overwhelms you. Its difficult.
Rhino crashed because a error happended in the subprocess
Using WebSocketSharp library is unusual, and misuse of it caused the error in the subprocess.
I know.
The whole story is that I want to dynamically run Rhino/Grasshopper code generated by a remote LLM service. Building a socket to recieve code is the first step. I want to fast build a runnable one and then build a stable one.