WebSocket服务器(Java)无法读取,只能发送到C#(Unity)客户端
对于大学的项目,我们应该建立一个多人游戏。因为我在Unity有很多经验,我想用它作为游戏引擎。我们必须使用WebSocket进行通信,并使用json作为传输数据的协议 到目前为止还不错。C#没有默认的websocket实现,因此我使用了一个名为websocket sharp的库来实现它() 我在客户端(Unity)的代码如下:WebSocket服务器(Java)无法读取,只能发送到C#(Unity)客户端,java,c#,http,unity3d,websocket,Java,C#,Http,Unity3d,Websocket,对于大学的项目,我们应该建立一个多人游戏。因为我在Unity有很多经验,我想用它作为游戏引擎。我们必须使用WebSocket进行通信,并使用json作为传输数据的协议 到目前为止还不错。C#没有默认的websocket实现,因此我使用了一个名为websocket sharp的库来实现它() 我在客户端(Unity)的代码如下: public class EchoTest : MonoBehaviour { public GameObject master; public
public class EchoTest : MonoBehaviour {
public GameObject master;
public string serveradress = "ws://";
private WebSocket w = null;
// Use this for initialization
IEnumerator Start () {
w = new WebSocket(new Uri(serveradress));
yield return StartCoroutine(w.Connect());
w.SendString ("Hello World from client");
//w.SendString("Hi there");
int i=0;
while (true)
{
string reply = w.RecvString();
if (reply != null)
{
master.GetComponent<Networker> ().onNetworkMessage (reply);
//Debug.Log ("Received: "+reply);
//w.SendString("Hi there"+reply);
}
if (w.error != null)
{
Debug.LogError ("Error: "+w.error);
break;
}
w.SendString("helloworld");
yield return 0;
}
w.Close();
}
public void sendString(string message){
sendRaw(Encoding.UTF8.GetBytes (message));
}
public void sendRaw(byte[] send){
w.Send (send);
}
}
没有问题:
Unity客户端无法向服务器发送数据!
他可以正确连接并从服务器读取消息,但他无法向他发送(触发器onmessage)。我找了一整天都没找到
客户机与在线服务以及服务器配合得很好,但两者都不起作用。有人能解释一下吗?一天后我发现了这个问题,我会为此而爱你的 WebSocket能够处理字符串、字节[]和其他一些格式 我发送的数据如下所示:
public void SendString(string str)
{
Send (Encoding.UTF8.GetBytes (str));
}
并使用底层的Send(byte[])方法。因此,当我将Java中的@OnMessage方法更改为
@OnMessage
public void processUpload(byte[] bytes, boolean last, Session session) {
一切正常,反过来我可以发送一个字符串,我的服务器也会收到我的消息:)
也许对某些人来说,知道是有用的
帕斯卡你好
@OnMessage
public void processUpload(byte[] bytes, boolean last, Session session) {