Browser and Server Communication Problem - Server only receive One text?
I try to build the website and communicate with Server (not web server), by using unity3D.
The problem is that, when I run both code on unity3d (one is client, other one Server). Both can send/receive test string very well.
[client side will send text:0,1,2,3,4,5,....]
[client side receive text:123,123]
[server side will send test:123,123. if server receive client side data]
[server side receive text:0,1,2,3,4,5,....]
But when I build the [website(client) code] to HTML5 by using Unity3D and run on Firefox browser, the server only receives one text data.
[server side receive the text:0] [client side no receive]
This is website(client) code:
public class EchoTest : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WebSocket w = new WebSocket(new Uri("ws://127.0.0.1:3000"));
yield return StartCoroutine(w.Connect());
w.SendString("0");
int i = 0;
while (true)
{
string reply = w.RecvString();
if (reply != null)
{
Debug.Log ("Received: "+reply);
w.SendString(""+i++);
}
if (w.error != null)
{
Debug.LogError ("Error: "+w.error);
break;
}
yield return 0;
}
w.Close();
}
}
This is Server code:
public class IOWebSocket : MonoBehaviour
{
private static int i = 0;
WebSocketServer server;
void Start()
{
server = new WebSocketServer(3000);
server.AddWebSocketService<Echo>("/");
server.Start();
}
void OnDestroy()
{
server.Stop();
server = null;
}
}
// Update is called once per frame
public class Echo : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
Sessions.Broadcast("123");
Debug.Log(System.Text.Encoding.Default.GetString(e.RawData));
}
}
I need some help, please.
Your answer
Follow this Question
Related Questions
WebGL browser error after run and built Unity+ARToolkit project 0 Answers
WebCamTexture on Mobile Browser 0 Answers
webgl + Facebook Canvas not working 0 Answers
Code to avoid or consider when exporting to WebGL? 0 Answers
WEBGL & input field on mobile 0 Answers