2013-07-10 32 views
6

Unity3d programımı node.js sunucusu ile bağlamak için socket.io kullanmaya çalışıyorum.Node.js ile Unity3d Bağlama

UnitySocketIO'yu kullanarak istemci ve sunucu arasındaki bağlantıyı başardım.

Ancak, On veya Emit yöntemi çalışmaz.

Birisi bana bu problemde yardımcı olabilir mi?

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 
} 

....

Sizin olay muhtemelen yanlış sırada ekli
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

Çalışıyor musunuz? Bu kendimle mücadele. – Jeff

+0

Sonunda bunu çözdünüz mü? UnitySocketIO'nun dll dosyalarını nereye koydunuz? – Nikola

+0

@Nikola, dll dosyalarını Assets klasörünüzün herhangi bir yerine koyun. Sonra (C# için) komut dosyasına sadece 'SocketIOClient;' kullanarak ekleyin. Ben javascript için eşdeğer ne olduğundan emin değilim ama anlamaya zor olamaz. – Harrison

cevap

2

böyle deneyin:

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(); 
} 

Ve düğüm için:

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

yanı müşterinin kendi kimliğine karar vermesine izin verme çoğu durumda. Sadece sunucu önemli kararlar vermeli ve müşteriyi değil.

+0

Yardımın için teşekkürler, ancak 'mesaj' için kayıt hala gelmiyor. – user1957032

+0

Örneğinizi nasıl daha kolay ayıklamak ve nasıl çalıştığını öğrenmek için örneklemenizi tamamen küçültmek ve ardından Unity uygulamanıza uygulamak isteyebilirsiniz. – moka