如何在Unity中使用SocketIOClient?

时间:2015-02-12 09:25:31

标签: c# android unity3d websocket socket.io

我想将Unity客户端的json数据发送到asp.net服务器。我在我的手机(android)上构建了应用程序,因此我需要在android平台上支持websocket。我不知道socketIO .Net服务器将使用什么,但它可能使用SignalR。是否有可能使用' SocketIO'在团结?那么,你们可以检查一下我的代码吗?

using UnityEngine;
using System.Collections;
using SocketIOClient;


public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    string socketUrl = "http://127.0.0.1:3000";
    Debug.Log("socket url: " + socketUrl);

            //Client client = new Client (socketUrl);
            //client.Send ("hello world!");

    Client client = new Client(socketUrl);

    client.Opened += SocketOpened;
    //client.Message += SocketMessage;
    //client.SocketConnectionClosed += SocketConnectionClosed;
    //client.Error +=SocketError;
    //string message = "Unity : message";
    string message = "Unity - message";
    client.Send(message);
    client.Connect();   
    }

private void SocketOpened(object sender, MessageEventArgs e) {
    //invoke when socket opened
    Debug.Log ("Hello");
}
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////

我决定使用Websocket-Sharp的问题。我也测试了asp.net服务器以发送json数据。它运作良好。 以下是我的代码。

using UnityEngine;
using System.Collections;
using WebSocketSharp;
using System.IO;

public class socket : MonoBehaviour {


// Use this for initialization
void Start () {
    //Debug.Log ("start");
    test ();
    //Debug.Log ("end");
}

// Update is called once per frame
void Update () {

}

private void test()
{
    // Create empty JSONOBJECT
    JSONObject j = new JSONObject (JSONObject.Type.OBJECT);
    // Add Field (ref. protocol.docx)
    j.AddField ("cType", "auto");
    j.AddField ("indexOrder", "1");
    j.AddField ("oderCount", "1");
    j.AddField ("arSpeed", "1000");
    j.AddField ("mSpeed", "1000 ");
    j.AddField ("arM1", "180");
    j.AddField ("arM2", "180");
    j.AddField ("arM3", "180");
    j.AddField ("arM4", "180");
    j.AddField ("arM5", "180");
    j.AddField ("arM6", "180");
    j.AddField ("emer", "0");
    j.AddField ("direcM1", "0");
    j.AddField ("speedM1", "0");
    j.AddField ("direcM2", "0");
    j.AddField ("speedM2", "0");
    //j.AddField ("rollDirecM1", "0");
    //j.AddField ("rollSpeedM1", "0");
    //j.AddField ("rollDirecM2", "0");
    //j.AddField ("rollSpeedM2", "0");

    string encodedString = j.Print ();
    Debug.Log (encodedString);

    WebSocket ws = new WebSocket("ws://192.172.0.1:3000");
    ws.OnMessage += (sender, e) =>
        Debug.Log("Laputa says: " + e.Data);
    ws.Connect();
    ws.Send (encodedString);
    //ws.Send("Unity!!");
}
}

2 个答案:

答案 0 :(得分:0)

我对Unity中的套接字知之甚少,我只是想弄清楚自己。但是,您确定要在Update()功能中创建客户端吗?在我看来,Unity将尝试每一帧创建一个全新的客户端。我想这可能是许多不同问题的原因。

可能是SocketIOClient代码失败并且WebSocket代码有效的原因是因为你有从Start()函数而不是Update()函数调用的WebSocket代码。

答案 1 :(得分:0)

我不确定你是否可以使用带有SocketIOClient的signalR。

我同意哈里森的观点。您应该将套接字创建放在start()函数中,并使用update()来处理套接字事件。