android - UnitySocketIO向nodejs发送websocket消息

标签 android node.js websocket unity-game-engine socket.io

只是想知道是否有人使用UnitySocketIO在unity和node.js之间聊天。

我想发送一条消息,但缺乏文档,每个人都有不同的方法来发送消息,但没有解释设置它的关键方面。

在 Unity(C#) 中我是这样设置的:

using UnityEngine;
using System.Collections;
using SocketIOClient;

public class Gui : MonoBehaviour {

    void OnGUI () {
        // Make a background box
        GUI.Box(new Rect(10,10,100,90), "Loader Menu");

        // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
        if(GUI.Button(new Rect(20,40,80,20), "Level 1")) 
        {
            Debug.Log("Button 1");
        }
        // Make the second button.
        if(GUI.Button(new Rect(20,70,80,20), "Socket test")) 
        {
            string socketUrl = "http://127.0.0.1:8001";
            Debug.Log("socket url: " + socketUrl);
            Client client = new Client(socketUrl);
            client.Send("Hello world");
        }
    }
}

我的 Node 服务器是这样设置来接收消息的:

io.sockets.on("connect", function(socket)
{
    socket.on("Hey", function(data)
    {
        console.log(data);
    });
});

我不确定它是如何工作的,我尝试将“嘿”统一发送到发送中,就像我与任何其他客户端一样,但它不允许多个参数。 任何见解都会很棒!

最佳答案

过去可以与 Socket.io 0.9.x 配合使用,但尚未尝试与 1.x 配合使用。 尝试在服务器端监听“message”事件。

io.sockets.on("connect", function(socket)
{
    socket.on("message", function(data)
    {
        console.log(data);
    });
});

您应该在数据中的某处看到“嘿”这个词。

关于android - UnitySocketIO向nodejs发送websocket消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615985/

相关文章:

java - Retrofit 中 RSS 提要解析期间的 ValueRequiredException

node.js - 使用nodejs将数据发布到odoo

ios - iOS 中的 Websocket 连接问题?

Node.js、ws 和 express-session : how to get session object from ws. upgradeReq

android - 如何在我的 Android 应用程序中打开 TIFF 文件?

java - 将API数据解析到TextView中

javascript - npm 安装出错

c++ - Boost beast::websocket 回调函数

java - Android:将欧盟货币解析为数字

node.js - 在 Azure 的 IISNode 上调用本地主机 URL