Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WebGL and Websocket of Unity 5 is working?

Discussion in 'WebGL' started by bakuman, Mar 18, 2015.

  1. bakuman

    bakuman

    Joined:
    Mar 18, 2015
    Posts:
    5
    Hello, To whom it may concern.

    I’d like to ask how to use “Websocket of Unity 5”.

    http://files.unity3d.com/jonas/WebSockets.unitypackage
    @jonas echterhoff

    http://blogs.unity3d.com/kr/2014/04/29/on-the-future-of-web-publishing-in-unity/

    https://github.com/sta/websocket-sharp

    I couldn’t connect anything to browser after making build by WebGL.

    Do you know what I already knew the WebGL doesn’t support “Socket” but I thought that “Websocket-sharp” library uses “System.Net.Socket” inside of library.

    Anyway, Here’s my question.
    Could you let me know the built asset can connect to server or not?
    Or, Can you teach me the solution for WebGL and Websockets about Unity5?

    Websocket-sharp => Websocket.cs => System.Net.Sockets.TcpClient => Socket
    [ Websocket.cs ]

    Code (csharp):
    1.  
    2. private void setClientStream ()
    3. {
    4.   ...
    5.   _tcpClient = new TcpClient (host, port);
    6.   ...
    7. }
    8. namespace System.Net.Sockets
    9. {
    10.   ...
    11.   public class TcpClient : IDisposable
    12.   {
    13.   ...
    14.   public Socket Client { get; set; }
    15.   ...
    16.   }
    17. }
    18.  
    If you need more information to answer this question, please let me know freely.


    Thank you.
     
    Last edited by a moderator: Mar 18, 2015
  2. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    The package includes websocket-sharp purely for use in the Editor and on non-WebGL platforms. When you do a WebGL build, that DLL is not included in the build - it uses a jslib plugin instead which uses the browser's WebSocket support.

    I think there is some sample code in the package to show how to use the plugin.

    WebSockets can't be used for connecting to a browser, they can only be used for connecting to a server. If you want browser-to-browser communication without a server then you need WebRTC. You still need a server to coordinate things, but you end up with a peer-to-peer connection. I posted a plugin for that here, which works via PeerJS: http://forum.unity3d.com/threads/unitypeerjs-simple-webrtc-support-for-unity-webgl.310166/
     
  3. bakuman

    bakuman

    Joined:
    Mar 18, 2015
    Posts:
    5
    Sorry... :-O

    The question was something wrong. (I corrected it)

    I want to know is "How to connect to a server and communicate with it in the WebGL Platform".

    In the Editor, it connected to a server and sended/receiveed some packets . No problem.
    When I built WebGL and tried in the browser, it couldn't connect to the server.

    Is there any solution?

    gfoot !! Thanks for answer. :)
     
  4. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Look in at the Javascript output in Firefox, and see if it's complaining about cross-domain requests or throwing up any other errors.
     
  5. bakuman

    bakuman

    Joined:
    Mar 18, 2015
    Posts:
    5
    I guess it is not concerned with Firefox or cross-domain.

    Log is below (Using websocket-sharp.dll. No problem in the Unity Editor test. Below is printed in the Firefox)

    // Start Test --------------------------------------------------
    LOG : Connect Try(WS://echo.websocket.org:80/)
    LOG : _tcpClient = new TcpClient("WS://echo.websocket.org", 80) => RUN !!
    ERROR: An exception has occured while connecting.
    Closed 1006.
    ERROR: An exception has occured while connecting.False
    ERROR: The Websocket connection has already been closed.
    // End Test --------------------------------------------------

    I guess "System.Net.Sockets" problem in the WebGL with Unity5.

    I'm c# programmer. :)

    So, I want to know the other class or library or plugin or assets or ... other ways.
    (No System.Net.Sockets... => no working in the WebGL)

    Can WebGL with Unity5 use Websocket with c#?
     
    Last edited: Mar 18, 2015
  6. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    You must not use websocket-sharp.dll in WebGL builds, it will not work. Instead you need to use Jonas's WebSocket.jslib plugin via the WebSocket.cs wrapper. Look at the echo example in the unitypackage to see how to use it.
     
  7. bakuman

    bakuman

    Joined:
    Mar 18, 2015
    Posts:
    5
    Ok. I will try Jonas's WebSocket.jslib.

    Thanks. gfoot. :)
     
    Last edited: Mar 18, 2015
  8. maxcc

    maxcc

    Joined:
    Mar 4, 2016
    Posts:
    1
    Yes. I encountered the same problem and WebSocket.jslib works.
    Tnx. you guys helped me a lot
     
  9. snarlynarwhal

    snarlynarwhal

    Joined:
    Jan 16, 2014
    Posts:
    29
    Assuming I use the WebSocket.jslib + WebSocket.cs wrapper for the front-end, I can still use websocket-sharp for the back-end right? Or does the no .Net/.Net.Sockets rule apply to both front-end and back-end when developing for WebGL?
     
  10. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Yes.

    the webgl client can't use .Net sockets. The back-end (assuming it's not webgl) can.
     
  11. snarlynarwhal

    snarlynarwhal

    Joined:
    Jan 16, 2014
    Posts:
    29
    Thank you!