Search Unity

WWW can not get response on WegGL player mode

Discussion in 'Web' started by Handsome-Wisely, Sep 13, 2017.

  1. Handsome-Wisely

    Handsome-Wisely

    Joined:
    Mar 20, 2013
    Posts:
    102
    i made a webgl project. and i use HttpListerner(c#) as my server and use port 8081.
    code like this:
    Code (CSharp):
    1.  sSocket = ar.AsyncState as HttpListener;
    2.                 HttpListenerContext context = sSocket.EndGetContext(ar);
    3.                 sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
    4.                 HttpListenerRequest request = context.Request;
    5.                 HttpListenerResponse response = context.Response;
    6.                 Stream strem = request.InputStream;
    7.                 StreamReader reader = new StreamReader(strem, Encoding.UTF8);
    8.  
    9.                 string info = reader.ReadToEnd();
    10.                 info = System.Web.HttpUtility.UrlDecode(info);
    11.                 reader.Close();
    12.                 strem.Close();
    13.  
    14.                 string res = procCls.onProcInfo(info);
    15.  
    16.                 byte[] buffer = Encoding.UTF8.GetBytes(res);
    17.                 response.ContentLength64 = buffer.Length;
    18.                 Stream output = response.OutputStream;
    19.                 output.Write(buffer, 0, buffer.Length);
    20.                 output.Close();
    21.                 response.Close();
    the function are save document data and so on. when i play in editor mode, it run ok. i can send message to server and get the response.send message like this:
    Code (CSharp):
    1. IEnumerator IEUpload(string _url)
    2.     {
    3.         byte[] myData = System.Text.Encoding.UTF8.GetBytes("This is some test data");
    4.         UnityWebRequest www = UnityWebRequest.Put(_url, myData);
    5.         yield return www.Send();
    6.  
    7.         if (!string.IsNullOrEmpty(www.error))
    8.         {
    9.             appendCtrlDebugTx("fail to request..." + www.error);
    10.             debugTx = "fail to request..." + www.error;
    11.             Debug.Log(www.error);
    12.         }
    13.         else
    14.         {
    15.             appendCtrlDebugTx(www.downloadHandler.text);
    16.             debugTx = www.downloadHandler.text;
    17.             Debug.Log("Upload complete!");
    18.         }
    19.     }
    but when i release project to a webgl and put it on a tomcate. i can only sent message to server but can not get response from server. and the error is unknown error.
    my tomcat port is 8080 my http server port is 8081. i make sure correct address and port. but i can not get response. how to do?
    thank you!
     
    iLyxa3D likes this.
  2. iLyxa3D

    iLyxa3D

    Joined:
    Sep 25, 2013
    Posts:
    31
    up.
    Same problem..

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         listener = new HttpListener ();
    4.         listener.Prefixes.Add ("http://localhost:4444/");
    5.         listener.Prefixes.Add ("http://127.0.0.1:4444/");
    6.         listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
    7.         listener.Start ();
    8.  
    9.         listenerThread = new Thread (startListener);
    10.         listenerThread.Start ();
    11.         Debug.Log ("Server Started");
    12.     }
    13.  
    14.     private void startListener ()
    15.     {
    16.         while (true) {            
    17.             var result = listener.BeginGetContext (ListenerCallback, listener);
    18.             result.AsyncWaitHandle.WaitOne ();
    19.         }
    20.     }
    ..lead to:

    Code (JavaScript):
    1. SocketException: Success
    2.   at System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) [0x00000] in <00000000000000000000000000000000>:0
    Unity 2019.3.14f
    WebGL Release build.