使用websocket连接Unity(客户端)和Python(服务器)

时间:2016-10-03 13:29:18

标签: python unity3d websocket cherrypy

我制作了一个程序,允许我每次按下一个单位的空格时发送一条消息,并使用cherrpy将它发送到python服务器。

问题是虽然我的'websocket.py'成功服务于localhost,但是一旦我启动了Unity播放器,它就会立即关闭错误。希望有一些帮助!!

websocket.py

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket

class Root(object):
    @cherrypy.expose
    def index(self):
        return 'some HTML with a websocket javascript connection'


    @cherrypy.expose
    def ws(self):
        # you can access the class instance through
        handler = cherrypy.request.ws_handler

class AgentServer(WebSocket):
    def opened(self):
        print("Opened!")

    def closed(self, code, reason):
        print("Closed!")

    def received_message(self,m):
        self.send(m.data,m.is_binary)

if __name__ == "__main__":
    cherrypy.config.update({
        "server.socket_host": "127.0.0.1",
        "server.socket_port": 3000,
    })
    WebSocketPlugin(cherrypy.engine).subscribe()
    cherrypy.tools.websocket = WebSocketTool()
    cherrypy.quickstart(Root(), "/", config={
        "/ws": {
            "tools.websocket.on": True,
            "tools.websocket.handler_cls": AgentServer,
        }
    })

Client.cs

using UnityEngine;
using System.Collections;
using WebSocketSharp;

public class Client : MonoBehaviour {
    private WebSocket ws;
    // Use this for initialization
    void Start () {
        this.ws = new WebSocket("ws://127.0.0.1:3000");
        this.ws.OnOpen += (sender, e) => {
            Debug.Log("Opened");
        };
        ws.OnMessage += (sender, e) =>
        {
            Debug.Log("WebSocket Message Type: " + e.Type + ", Data: " + e.Data);
        };
        this.ws.OnClose += (sender, e) => {
            Debug.Log("Closed");
        };
        this.ws.Connect();
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyUp(KeyCode.Space))
        {
            Debug.Log ("Pressed");
            ws.Send("Test Message");
        }

    }
    void OnDestroy()
    {
        ws.Close();
        ws = null;
    }
}

Unity中的ErrorCode

Closed
UnityEngine.Debug:Log(Object)
client:<Start>m__3E(Object, CloseEventArgs) (at Assets/client.cs:18)
WebSocketSharp.Ext:Emit(EventHandler`1, Object, CloseEventArgs) (at Assets/Packages/websocket-sharp/Ext.cs:1101)
WebSocketSharp.WebSocket:close(CloseEventArgs, Boolean, Boolean, Boolean) (at Assets/Packages/websocket-sharp/WebSocket.cs:917)
WebSocketSharp.WebSocket:fatal(String, CloseStatusCode) (at Assets/Packages/websocket-sharp/WebSocket.cs:1128)
WebSocketSharp.WebSocket:doHandshake() (at Assets/Packages/websocket-sharp/WebSocket.cs:1085)
WebSocketSharp.WebSocket:connect() (at Assets/Packages/websocket-sharp/WebSocket.cs:958)
WebSocketSharp.WebSocket:Connect() (at Assets/Packages/websocket-sharp/WebSocket.cs:2472)
client:Start() (at Assets/client.cs:20)

Anaconda提示-python websocket.py

[03/Oct/2016:22:11:59] ENGINE Listening for SIGTERM.
[03/Oct/2016:22:11:59] ENGINE Bus STARTING
[03/Oct/2016:22:11:59] ENGINE Set handler for console events.
[03/Oct/2016:22:11:59] ENGINE Starting WebSocket processing
[03/Oct/2016:22:11:59] ENGINE Started monitor thread '_TimeoutMonitor'.
[03/Oct/2016:22:11:59] ENGINE Started monitor thread 'Autoreloader'.
[03/Oct/2016:22:11:59] ENGINE Serving on http://127.0.0.1:3000
[03/Oct/2016:22:11:59] ENGINE Bus STARTED
127.0.0.1 - - [03/Oct/2016:22:12:03] "GET / HTTP/1.1" 200 48 "" "websocket-sharp/1.0"

0 个答案:

没有答案