2013-07-10 10 views
6

私はunity3dプログラムをnode.jsサーバに接続するためにsocket.ioを使用しようとしています。Unity3dとNode.jsの接続

UnitySocketIOを使用して、クライアントとサーバーの間の接続に成功しました。

ただし、OnまたはEmitメソッドは機能しません。

誰かがこの問題を解決できますか?

void Start() { 

    string socketUrl = "http://127.0.0.1:50122"; 
    Debug.Log("socket url: " + socketUrl); 

    this.socket = new Client(socketUrl); 
    this.socket.Opened += this.SocketOpened; 
    this.socket.Message += this.SocketMessage; 
    this.socket.SocketConnectionClosed += this.SocketConnectionClosed; 
    this.socket.Error += this.SocketError; 

    this.socket.Connect(); 
} 

private void SocketOpened (object sender, EventArgs e) { 
    Debug.Log("socket opened");     // i got this message 
    this.socket.On ("message", (data) => { 
     Debug.Log ("message : " + data); 
    }); 
    this.socket.Emit("join", "abc"); 
    Debug.Log("Emit done");     // i got this message 
} 

....

io.sockets.on('connection', function (socket) { 

    console.log('connect');     // i got this message 

    socket.emit('message', 'Hello World!'); 

    socket.on('join', function (id) { 
     console.log('client joined with id ' + id); 
     socket.emit('message', 'Hello ' + id); 
    }); 
}); 
+0

この作品を購入できますか?これで自分自身で苦闘する。 – Jeff

+0

これを最後に解決しましたか? UnitySocketIOのdllファイルはどこに置いたのですか? – Nikola

+0

@Nikola、あなたのAssetsフォルダのどこにでもdllファイルを置いてください。次に(C#の場合) 'using SocketIOClient;'をスクリプトに追加するだけです。私は同等のものがjavascriptのために何であるかは分かりませんが、理解するのは難しくありません。 – Harrison

答えて

2

おそらく間違った順序で取り付けたあなたのイベント、次のように試してみてください。同様に

io.sockets.on('connection', function(socket) { 
    console.log('client connected'); 
    socket.emit('message', 'Hello World!'); 
}); 

void Start() { 
    string socketUrl = "http://127.0.0.1:50122"; 
    Debug.Log("socket url: " + socketUrl); 

    this.socket = new Client(socketUrl); 
    this.socket.Opened += this.SocketOpened; 
    this.socket.Message += this.SocketMessage; 
    this.socket.SocketConnectionClosed += this.SocketConnectionClosed; 
    this.socket.Error += this.SocketError; 

    this.socket.On("message", (data) => { 
     Debug.Log("message: " + data); 
    }); 

    this.socket.Connect(); 
} 

とノードのためにクライアントが自分のIDを決めることを許可しないほとんどの場合「kable」になります。サーバーだけがクライアントではなく重要な意思決定を行う必要があります。

+0

助けてくれてありがとうが、 'メッセージ'のログはまだ出てこない。 – user1957032

+0

あなたのサンプルを完全に最小限に抑えて、簡単にデバッグして動作を見つけて、Unityアプリケーションに実装することができます。 – moka

関連する問題