Search Unity

Resolved How connect to server in WebGL

Discussion in 'Multiplayer' started by Serhii-Horun, Jan 17, 2016.

  1. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Hi guys!
    I looked at several projects(created by Unity Tech), but I still did not understand how to connect some Match in WebGL. My steps:
    1. useWebSockets = true
    2. build for WebGL
    3. Run in editor and browser
    4. LAN Host in redactor and LAN client in browser. Its work
    What I want:
    1.Create match(in Editor or any possible Platform. I know that you cant lan host in WebGL, only connect)
    2.Find match in browser
    3.Connect

    link to NetworkLobby Example from Unity tech(its like Tanks Example)
    https://www.assetstore.unity3d.com/en/#!/content/41836

    Someone can tell me how I can do it? (find and connect to internet match from WebGL).
    Thanks in advance!
     
    Last edited: Jan 17, 2016
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Their match maker doesn't work with WebGL game. If you host a server in editor and tick the use websocket in network manager or set it true in NetworkServer and open the client in Firefox then it should work, On Edge and chrome you should use build and run for it to be hosted in a local web server to work.
     
  3. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Maybe I dont understand well. For example, I build WebGL project and run it on Firefox on my friend's PC. I run server in Editor (LAN Host) on my PC. Then my friend try connect to server(using my IP), but nothing happens. When I try do the same at the same PC, its work nicely.

    Briefly : computer1-computer1 = work nicely, but
    computer1-computer2 = cant find/connect to match

    How my friends can connect(WebGL) to my macth(Server in editor) ?
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Aha , This is due to security issues.
    You need a cross domain reference policy file which allows the connection to happen I guess. check the webGL part of the manual in relation to multiplayer.
     
    Serhii-Horun likes this.
  5. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Thanks man! If it will work - you saved my life!! :):) ;)
     
  6. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Actually I was wrong, cross origin resource sharing seems to only apply to HTTP requests.
    See the manual page related to it
    http://docs.unity3d.com/Manual/webgl-networking.html

    In the coming days I should make this workin so can update you, if I forgot, send me a private message. I'm working on a cross mobile and WebGL multiplayer product so I have to make this work today or tomorrow and it should work.
     
  7. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Yeah, thank you!! I'll look forward to.:)
     
  8. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    272
    Did anything happen here?
     
  9. Danila24ru

    Danila24ru

    Joined:
    Sep 21, 2012
    Posts:
    17
    Any solution? :eek:
    Just give me idea how I can connect to non-localhost ip address this thing. >_<
    Browser : Chrome and Firefox
    Host : github.io
    Unity: 2017.2.0b5

     
    Last edited: Aug 3, 2017
  10. mofirouz

    mofirouz

    Joined:
    Jul 14, 2015
    Posts:
    27
    @Danila24ru Your error is different to the error mentioned in the rest of the thread. The connection string you use in your unity scene should begin with "wss://" which indicates that you are trying to connect with SSL/HTTPS secured connection.

    As for connecting to a non-local IP address/host, the problem you are encountering is related to CORS support, which is a built-in security functionality of most browsers. There are a couple of ways you can get around this:

    - You could start Chrome in insecure mode. This answer shows how to start in insecure mode.
    - You'll need to update the server code to allow requests to come through from custom domains. or you can use third party servers that allow custom domains.
     
  11. riccardo2697

    riccardo2697

    Joined:
    Jan 31, 2020
    Posts:
    1
    hi, i have the same problem, did you find a solution?
     
  12. iprototech

    iprototech

    Joined:
    Feb 14, 2018
    Posts:
    1
    you found the solution ? Can you help me %
     
  13. parhamebi1

    parhamebi1

    Joined:
    Aug 21, 2023
    Posts:
    1
    hello @danila24 did u found any solution?
     
  14. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    27
    Hello @parhamebi1,

    If mofirouz's answer didn't solve your issue, please post a new thread into the WebGL section, you'll have more chances of getting help.

    Thanks!