C# 如何在Unity中使用SocketIOClient?

C# 如何在Unity中使用SocketIOClient?,c#,android,unity3d,websocket,socket.io,C#,Android,Unity3d,Websocket,Socket.io,我想将json数据从unity客户端发送到asp.net服务器。我已经在我的手机(android)上构建了这个应用程序,所以我需要android平台上支持的websocket。我不知道socketIO.Net服务器将使用什么,但它可能使用信号器。我有可能在unity中使用“SocketIO”吗?那你们能查一下我的密码吗 using UnityEngine; using System.Collections; using SocketIOClient; public class NewBeha

我想将json数据从unity客户端发送到asp.net服务器。我已经在我的手机(android)上构建了这个应用程序,所以我需要android平台上支持的websocket。我不知道socketIO.Net服务器将使用什么,但它可能使用信号器。我有可能在unity中使用“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!!");
}
}

我对Unity中的套接字不太了解,我只是想自己弄明白。但是,是否确实要在
Update()
函数中创建客户端?在我看来,Unity将尝试在每一帧中创建一个全新的客户端。我想这可能是许多不同问题的原因


SocketIOClient代码失败和WebSocket代码正常工作的原因可能是因为您使用了从
Start()
函数调用的WebSocket代码,而不是从
Update()
函数调用的WebSocket代码。

我不确定是否可以将signar与SocketIOClient一起使用


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

您使用哪个版本的Unity?Unity Pro还是免费?@Barışırıka我正在使用Unity Pro版本。我原以为Unity支持websocket库,但我在尝试“使用”时找不到任何与websocket相关的东西。所以我很难使用“websocket sharp”,但它没有足够的样本。。。。我对服务器或客户端不是很专业。你知道吗?你可以用