Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best HTTP Released

Discussion in 'Assets and Asset Store' started by BestHTTP, Sep 11, 2013.

  1. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    @BestHTTP Loving your product so far! I am using it for the socket.io functionality.

    Are there any known issues with CORS for the WebGL platform? My socket server has cors allowed for everything. In the editor, on android, on desktop, the sockets all work fine. Even on chrome it works half the time.

    In firefox and sometimes chrome, the pre-flight OPTIONS request fails due to a 400 Bad Request.
    My WebGL build is in an iframe on a different domain than the socket server, the javascript wrapper for the iframe (which is also not on the same domain as the socket server) can connect to the socket server no problem so I know it is not a server issue.

    From what i've looked into, the request for the pre-flight OPTIONS in firefox is different than the chrome request that works, maybe if theres a way to edit the request object for socket.io connections?

    If you have any ideas please let me know. I can send you a link to a place to see it error if you need more info. Thanks!
     
  2. cemozturk

    cemozturk

    Joined:
    Sep 27, 2015
    Posts:
    31
    Hello,

    I purchased pro edition but i'm using the library in plugin mode to get faster compile time in Unity.

    When i send message to server via Socket.IO i'm using

    manager.Socket.Emit("clientEvent", data.ToString()); and parse the string to the object at server.

    But when i wanted to send direct json-object, your library throws "Encoding the arguments to JSON failed!". I did fix it in pro edition source file by extending Emit method but is there anyway to do that in plugin version ?


    note : This is how i accomplished with socket.cs, now i do not need to JSON,parse the string in server.

    public Socket Emit(string eventName, JSONObject jsonobj)
    {
    JSONObject pack = new JSONObject(JSONObject.Type.ARRAY);
    pack.Add(eventName);
    pack.Add(jsonobj);

    Packet packet = new Packet(TransportEventTypes.Message,
    SocketIOEventTypes.Event,
    this.Namespace,
    pack.ToString(),
    0,
    0);

    (Manager as IManager).SendPacket(packet);

    return this;
    }
     
    Last edited: Feb 23, 2017
  3. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Jribeira

    The plugin uses the underlying browser's XMLHttpRequest on WebGL. I know that FireFox has very strict rules for CORS.
    The browser side of the plugin is located in the \Assets\Best HTTP (Pro)\Plugins\WebGL\BestHTTP_WebRequest.jslib.

    (As these are javascript functions they are not compiled, instead included in the final build as is. You can search for XHR_Create in the final WebGL.js in your build directory. This way it's much easier to debug small changes.)

    Let me know if you find something.
     
  4. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @cemozturk

    I'm pretty sure you can do the same with extension methods:

    Code (CSharp):
    1. public Socket Emit(this Socket socket, string eventName, JSONObject jsonobj)
    2. {
    3.     JSONObject pack = new JSONObject(JSONObject.Type.ARRAY);
    4.     pack.Add(eventName);
    5.     pack.Add(jsonobj);
    6.  
    7.     Packet packet = new Packet(TransportEventTypes.Message,
    8.                                 SocketIOEventTypes.Event,
    9.                                 socket.Namespace,
    10.                                 pack.ToString(),
    11.                                 0,
    12.                                 0);
    13.  
    14.     (socket.Manager as IManager).SendPacket(packet);
    15.  
    16.     return socket;
    17. }
     
    Last edited: Feb 23, 2017
  5. Elfinnik159

    Elfinnik159

    Joined:
    Feb 24, 2013
    Posts:
    145
    Hello.
    I'm looking for a solution.
    I have a game server (written in unity), there is a PC client, which play through my server.
    Now the network is written by LLAPI
    I want to migrate players in a social network, but LLAPI can not work in WebGL to connect via SSL (wss / https). But social networks only work with https.
    Do I fit Best HTTP (Pro Edition)?
     
  6. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Elfinnik159

    The plugin uses the underlying browser's JavaScript API for regular requests and for WebSockets, so SSL isn't a problem in WebGL builds. However you can't use it as a low level (socket) API. But if you want to migrate to a higher level API, you can use the very same WebSocket/Socket.IO/SignalR/etc. API in every build with the plugin, so in this case it can be a good fit.
     
  7. Xemenas

    Xemenas

    Joined:
    Sep 10, 2012
    Posts:
    28
    Hi,

    My company has a WebGL project, and we've just uploaded it to our server with the backend functionality enabled, but our login request(s) are returning with an "Unsupported Media Type" error. However, all of the methods work in the Editor, and bizarrely work on a couple users' Firefox browsers, but overall (on all Chrome, Edge, I.E. and Safari browsers) it returns with this error.

    Is this a known issue / an issue with the plugin, or should I try to have the backend team look into it?

    Thank you!
     
  8. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Xemenas

    There are no known issues that could cause something like this.
    Most of the references I found mentions missing "Accept" header. You could try out adding a request.SetHeader("Accpet", "application/json"); (or whatever mime-type you are expecting to return from the server).
    But it isn't set in the editor neither so I don't know why it would cause issues only on WebGL...
     
  9. Xemenas

    Xemenas

    Joined:
    Sep 10, 2012
    Posts:
    28
    So I didn't notice that the Content-Type header was being set twice. Once I removed the extra instance it started working. If you want me to upload some code so you can test the issue, let me know!
     
  10. hoyeon-c

    hoyeon-c

    Joined:
    Jul 8, 2015
    Posts:
    4
    I tried to connect again after disconnecting websocket.
    The connection has received a completed event and attempted to transmit data but it is not transmitted.
    After temporarily calling "HTTPManager.OnQuit ()", it works fine, but Unity stops.
     
  11. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @hoyeon-c

    When the websocket disconnected, are you trying to re-open the disconnected websocket?
     
  12. li76168

    li76168

    Joined:
    Sep 7, 2014
    Posts:
    11
    Hello,

    Recently there is a problem, I try to solve it many days. My game app is using Websocket, when the game running,it will connect to the login, chat, game servers with 3 clients,and login, chat connected very soon, but the game connected after about 20 second,the error message is "Time out", this problem only in ios. After game connected once,next time reconnect, it connected like login and chat very soon,sorry for my English.

    PS: login,chat,game are the same IP and different port services.

    Thanks.
     
  13. axot

    axot

    Joined:
    Apr 20, 2015
    Posts:
    4
    Is it possible using iOS/Android system side http proxy settings ?
     
    Last edited: Mar 1, 2017
  14. li76168

    li76168

    Joined:
    Sep 7, 2014
    Posts:
    11
    @axot

    I had try wifi, 4G and different devices, the problem is the same.

    thanks
     
  15. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @li76168

    Can you send me log created with detailed logging? You can set the loglevel:
    Code (CSharp):
    1. BestHTTP.HTTPManager.Logger.Level = BestHTTP.Logger.Loglevels.All;
    (Or if it's possible a repro-project could do it too.)
     
  16. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @axot

    The plugin can't access/use the proxy settings as it would require native code that i try to avoid, but it looks possible.
     
  17. axot

    axot

    Joined:
    Apr 20, 2015
    Posts:
    4
    @BestHTTP

    I checked out the link you said above, maybe it depends how the connection implementation of C# side.
    Any way I will try it and report the result later.
    Thank you.
     
  18. li76168

    li76168

    Joined:
    Sep 7, 2014
    Posts:
    11
    Ok,I will do it.

    I google many keywords found this link http://blog.lightstreamer.com/2013/01/on-ios-url-connection-parallelism-and.html, is it possible the reason about my problem? but I checked Xcode profile, I only used 2 connections.
    The third connection will be timeout above 5 times then connected.
     
  19. axot

    axot

    Joined:
    Apr 20, 2015
    Posts:
    4
    Last edited: Mar 2, 2017
  20. navnahn

    navnahn

    Joined:
    Mar 2, 2017
    Posts:
    7
    @BestHTTP
    I tried to used Socket.IO in bestHTTP to connect to the nodejs server use socket.io library.
    But I got the error when open the connection.

    The client code in unity:
    Code (CSharp):
    1. using BestHTTP.SocketIO;
    2. using System;
    3.  
    4. public class test : MonoBehaviour {
    5.    
    6.     private SocketManager manager;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         // Change an option to show how it should be done
    11.         SocketOptions options = new SocketOptions();
    12.         options.AutoConnect = false;
    13.  
    14.  
    15.         // Create the Socket.IO manager
    16.         manager = new SocketManager(new Uri("http://127.0.0.1:4010/test/"), options);
    17. //        Socket nsp = manager.GetSocket("/test");
    18.  
    19.         //connect to server
    20.         manager.Open();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.    
    26.     }
    27. }
    The server code:
    Code (JavaScript):
    1. "use strict";
    2.  
    3. var io = require('socket.io').listen(4010);
    4.  
    5. var names = io.of('/test');
    6.  
    7.  
    8. io.on('connect', function (socket) {
    9.     console.log("connection "+ socket.id);
    10. });
    11.  
    12. names.on('connect', function (socket) {
    13.  
    14.     console.log("connection "+ socket.id);
    15.  
    16.  
    17.     socket.on('hello', function (data, resp) {
    18.         console.log(data);
    19.         resp({return: 'server return'});
    20.     });
    21.  
    22. });
    The error in unity:
    I am using the bestHTTP pro edition v 1.9.4, socket.io 1.7.3 and nodejs on server sidde.
     
  21. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @axot

    By default the plugin will send the whole uri for the proxy, but you can disable this behavior:
    Code (CSharp):
    1. BestHTTP.HTTPManager.Proxy = new BestHTTP.HTTPProxy(new Uri("http://sample.domain.com:8080"), /*credentials: */ null, /*isTransparent: */ false, /*sendWholeUri: */ false);
     
  22. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    Socket.IO will be bound to the /socket.io/ path by default, so you have to connect like this:
    Code (CSharp):
    1. manager = new SocketManager(new Uri("http://127.0.0.1:4010/socket.io/"), options);
    2. Socket nsp = manager.GetSocket("/test");
     
    navnahn likes this.
  23. navnahn

    navnahn

    Joined:
    Mar 2, 2017
    Posts:
    7
    @BestHTTP
    thanks for your answer!
    I fixed it.
    I have another question about this problem.
    When I run by unity, everything is OK, I could connect to server and receive the events which sent from server.
    But, when I build in Android and iOS platform, I got the errors:
    In iOS:
    in Android:
    Edit:
    Sorry, this my fail. I set wrong ip address.
     
    Last edited: Mar 2, 2017
  24. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    I think your server listens for connections on your localhost only!
     
  25. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    And you shouldn't try to connect to 127.0.0.1 on a client device, it will try to connect to the device itself.
     
  26. navnahn

    navnahn

    Joined:
    Mar 2, 2017
    Posts:
    7
    @BestHTTP
    I fixed it and it works OK on android device, but on iOS device, I get error:
    Thanks you for your quick reply on my question :D
     
  27. haswalt

    haswalt

    Joined:
    Oct 28, 2016
    Posts:
    2
    @BestHTTP Quick question:

    Does this support SNI? Using lets-encrypt certs don't work with Unity and we don't really want to roll our own HTTP client.
     
  28. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    You can increase the default connect timeout:
    Code (CSharp):
    1. BestHTTP.HTTPManager.ConnectTimeout = System.TimeSpan.FromSeconds(120);
    Or you can turn off connect timeouts:
    Code (CSharp):
    1. BestHTTP.HTTPManager.ConnectTimeout = System.TimeSpan.Zero;
     
  29. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @haswalt

    Yes, SNI supported by default.
     
  30. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    I would like to use strongly typed objects instead of MethodCallMessage as mentioned in post #660. I have 50 server APIs and "Best HTTP" most robust approach seems to be manually mapping in a derived Hub class. SignalR 2.1 server offers strong typed hubs in the form "public class StrongHub : Hub<IClient>". It would be nice if "Best HTTP" could make use of IClient as intended and keep MethodCallMessage internal. A complete solution would incorporate IServer for strongly typed calls to the server. Do you plan to implement strong typing in the near term? Thanks for this great asset:)
     
  31. Zanderfax

    Zanderfax

    Joined:
    Nov 2, 2016
    Posts:
    25
    Hello,

    I just purchased BestHTTP and am trying to connect to namespaces. I have two questions:

    1) Do I need a separate manager for each namespace or will the code below work?

    skMgr = new SocketManager(new Uri("http://127.0.0.1:9999/socket.io/"));
    tablesInfra = skMgr.GetSocket("/tables_infra");
    tablesChat = skMgr.GetSocket("/tables_chat");
    gamesInfra = skMgr.GetSocket("/games_infra");

    2) What is the proper syntax to emit to a namespace? With the code above I try to use the following to emit a message without success:

    tablesInfra.Emit("skMgrTest");

    Any guidance would be greatly appreciated,

    Z
     
  32. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @aidesigner

    The official client (and server) does a lot of reflection to achieve strongly typed hubs without any additional coding.
    As my plugin targets mobile platforms I felt this approach too cumbersome. So, I take my vote on better performance but it comes with more coding.

    If you want, you can still catch all method calls by subscribing to the OnMethodCall of the Hub. Here you can use reflection and parameter conversions to call a hub function. You have to write this OnMethodCall callback once and you can use this hub implementation as a base class for your actual hubs.
     
  33. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Zanderfax

    1.) No, and your sample should work.
    2.) It should work as you tried, after the connect event.

    Modified a little the chat example's client (\Assets\Best HTTP (Pro)\Examples\SocketIO\SocketIOChatSample.cs) and server side.

    On client side added these:
    Code (CSharp):
    1. Manager.Socket.On(SocketIOEventTypes.Error, (socket, packet, args) => Debug.LogError(string.Format("Error: {0}", args[0].ToString())));
    2.  
    3. var chatSocket = Manager.GetSocket("/chat");
    4. chatSocket.On(SocketIOEventTypes.Connect, (socket, packet, arg) => {
    5.     Debug.LogWarning("Connected to /chat");
    6.  
    7.     chatSocket.Emit("testmsg", "Message from /chat 'on connect'");
    8. });
    9.  
    10. Manager.GetSocket("/other").On(SocketIOEventTypes.Connect, (socket, packet, arg) =>
    11. {
    12.     Debug.LogWarning("Connected to /other");
    13.  
    14.     socket.Emit("testmsg", "Message from /other 'on connect'");
    15. });
    16.  
    17. // We set SocketOptions' AutoConnect to false, so we have to call it manually.
    18. Manager.Open();
    upload_2017-3-11_14-21-43.png

    On server side (index.js) added a /chat and /other namespaces. When a client connected it will listen for 'testmsg' events and will log out the message:
    Code (CSharp):
    1. var numUsers = 0;
    2.  
    3. var chat = io.of('/chat');
    4.  
    5. chat.on('connection', function (socket) {
    6.     console.log('New chat connection: ' + socket.id);
    7.    
    8.     socket.on('testmsg', function (msg) {
    9.         console.log(socket.id + ' /chat testmsg: ' + msg);
    10.     });
    11. });
    12.  
    13. io.of('/other').on('connection', function (socket) {
    14.     console.log('New other connection: ' + socket.id);
    15.    
    16.     socket.on('testmsg', function (msg) {
    17.         console.log(socket.id + ' /other testmsg: ' + msg);
    18.     });
    19. });
    20.  
    21. io.on('connection', function (socket) {
    upload_2017-3-11_14-23-26.png
     
  34. Zanderfax

    Zanderfax

    Joined:
    Nov 2, 2016
    Posts:
    25
    Thanks you for the quick response! I was sending the message too early, before the connections.
    Sometimes Async is hard!

    Now to wire up Unity to my server!

    Z
     
  35. Zanderfax

    Zanderfax

    Joined:
    Nov 2, 2016
    Posts:
    25
    One additional question:

    If I am only using the Socket.IO part of the Asset, what can I safely remove during import to minimize what I am loading?

    Z
     
  36. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Zanderfax

    You can exclude the Examples folder. Other than this, you can disable features of the plugin by using platform dependent compilation. You can gain big by adding BESTHTTP_DISABLE_ALTERNATE_SSL, but after this the plugin will fall back to mono's SslStream implementation that rather old now.

    Other defines will remove much, much less code, but you can add all of them except _DISABLE_WEBSOCKET and _DISABLE_SOCKETIO.

    Additionally, I would recommend to connect to your server straight with WebSocket by using the ConnectWith property of the SocketOptions:
    Code (CSharp):
    1. SocketOptions options = new SocketOptions();
    2. options.AutoConnect = false;
    3. #if !BESTHTTP_DISABLE_WEBSOCKET
    4. options.ConnectWith = BestHTTP.SocketIO.Transports.TransportTypes.WebSocket;
    5. #endif
    6.  
    7. // Create the Socket.IO manager
    8. Manager = new SocketManager(new Uri("http://localhost:3000/socket.io/"), options);
    It will speed up initial connection.
     
  37. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    Unable to make a signalr connection to my localhost C# console signalr server, but your demo seems to work. Would you mind taking a look at my setup/error below. When I run the server I get the expected console window with message "Server running at http://localhost:8080/". Then I start my simple unity BestHttp signalr client and receive error below. I tried ports other than 8080, but it did not help.

    Unity error message:
    Err [NegotiationData]: Negotiation request failed with error: Negotiation request finished Successfully, but the server sent an error. Status Code: 500-Internal Server Error Message: Uri: http://localhost:8080/signalr/negotiate?tid=0&_=1156542530&clientProtocol=1.5&connectionData=[{"Name":"AvatarHub"}]
    UnityEngine.Debug:LogError(Object)

    Unity Signalr client:
    public class TestSignalR : MonoBehaviour {

    // Use this for initialization
    void Start () {
    HTTPManager.Logger.Level = Loglevels.All;
    Hub hub1 = new Hub("AvatarHub");
    Connection signalRConnection = new Connection(new Uri("http://localhost:8080/signalr"), hub1);
    signalRConnection.Open();
    }
    }

    C# Console App server code:
    class Program {
    static void Main(string[] args)
    {
    using (WebApp.Start<Startup>("http://localhost:8080")) {
    Console.WriteLine("Server running at http://localhost:8080/");
    Console.ReadLine();
    }
    }
    }

    class Startup {
    public void Configuration(IAppBuilder app) {
    app.UseCors(CorsOptions.AllowAll);
    app.MapSignalR();
    }
    }

    class AvatarHub : Hub<IAvatarClient> {
    public void ServerFunction(string argument) {
    System.Diagnostics.Debug.WriteLine("AvatarHub: ServerFunction");
    Clients.All.ClientFunction(argument);
    }
    }

    Thanks,
    aidesigner
     
    Last edited: Mar 12, 2017
  38. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @aidesigner

    Following this tutorial (with VS2015, i think version numbers shouldn't matter) created a server:
    Code (CSharp):
    1. using System;
    2. using Microsoft.AspNet.SignalR;
    3. using Microsoft.Owin.Hosting;
    4. using Owin;
    5. using Microsoft.Owin.Cors;
    6. using Microsoft.Owin;
    7.  
    8. namespace SignalRSelfHost
    9. {
    10.     class Program
    11.     {
    12.         static void Main(string[] args)
    13.         {
    14.             string url = "http://localhost:8080";
    15.             using (WebApp.Start<Startup>(url))
    16.             {
    17.                 Console.WriteLine("Server running on {0}", url);
    18.                 Console.ReadLine();
    19.             }
    20.         }
    21.     }
    22.  
    23.     class Startup
    24.     {
    25.         public void Configuration(IAppBuilder app)
    26.         {
    27.             app.UseCors(CorsOptions.AllowAll);
    28.             app.MapSignalR();
    29.         }
    30.     }
    31.  
    32.     public class AvatarHub : Hub<IAvatarClient>
    33.     {
    34.         public void ServerFunction(string argument)
    35.         {
    36.             System.Diagnostics.Debug.WriteLine("AvatarHub: ServerFunction - argument: " + argument);
    37.             Clients.All.ClientFunction(argument);
    38.         }
    39.     }
    40.  
    41.     public interface IAvatarClient
    42.     {
    43.         void ClientFunction(string arg);
    44.     }
    45. }
    (You can skip the javascript client creation.)

    And my client:
    Code (CSharp):
    1. void SignalRSelftHostTest()
    2. {
    3.     Hub hub1 = new Hub("AvatarHub");
    4.     BestHTTP.SignalR.Connection signalRConnection = new BestHTTP.SignalR.Connection(new Uri("http://localhost:8080/signalr"), hub1);
    5.     signalRConnection.OnConnected += SignalRConnection_OnConnected;
    6.     signalRConnection.Open();
    7. }
    8.  
    9. private void SignalRConnection_OnConnected(BestHTTP.SignalR.Connection connection)
    10. {
    11.     connection.Hubs[0].Call("ServerFunction", "Sending From Client!");
    12. }
    13.  
    Calling SignalRSelfHostTest() resulted in this log on the server console:
    upload_2017-3-12_23-36-33.png

    And the plugin logged out an unhandled hub message (as expected):
    upload_2017-3-12_23-36-59.png
     
  39. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    Thanks for the help as your answer was very detailed. I also pulled my signalr server from the same tutorial, so our code was almost the same. The one difference was that my Hub class was missing the "public" modifier. This was the source of the failure as I guess signalr internal reflection could not find the hub. Thanks again...
     
  40. navnahn

    navnahn

    Joined:
    Mar 2, 2017
    Posts:
    7
    @BestHTTP
    I read the document online and found this:
    "error": Sent on server or internal plugin errors. The event’s only argument will be a BestHTTP.SocketIO.Error object.

    But when I used it inside my code like this:
    Code (CSharp):
    1. socket.On ("error", (socket_, originalPacket, args) => {
    2.             //save that this socket is connecting to server
    3.             Debug.LogError ("Connection error with string");
    4.             this.isStillConnect = false;
    5.            
    6.         });
    I cannot catch this event when I try to connect to a server (are offline at this time).
    I saw the exception which printed in the console of unity like that:
    Can you help me point out what wrong with my code?
    Thank you!
     
  41. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    Modified my chat sample to try to connect to a nonexistent port on my localhost, the error handler called as expected:
    Code (CSharp):
    1. Manager.Socket.On(SocketIOEventTypes.Error, (socket, packet, args) => {
    2.     Debug.LogError(string.Format("Error: {0}", args[0].ToString()));
    3. });
    Is your 'socket' is the root ('/') socket?
     
  42. navnahn

    navnahn

    Joined:
    Mar 2, 2017
    Posts:
    7
    @BestHTTP
    I read your example and follow it.

    The code when I connect to server:
    Code (CSharp):
    1. SocketOptions options = new SocketOptions();
    2.         options.AutoConnect = false;
    3.         options.ReconnectionAttempts = 20;
    4.         BestHTTP.HTTPManager.ConnectTimeout = System.TimeSpan.FromSeconds(30);
    5. //        uri = "http://54.161.70.31:4010/socket.io/";
    6.  
    7.         this.uri = uri+"/socket.io/";
    8.  
    9.         socketManager = new SocketManager(new Uri(this.uri), options);
    10.  
    11.         socket = socketManager.GetSocket("/gameserver");
    I could catch the connected, disconnected event and events which emit from server:
    Code (CSharp):
    1. socket.On (SocketIOEventTypes.Connect,
    2.             (socket_, originalPacket, args) => {
    3.                 if (ENABLE_TEST_PROTOBUFSERVERMANAGER) {
    4.                     Debug.LogError("Connect Ok");
    5.                 }
    6.                
    7.                    
    8.                
    9.                 //save that this socket is connecting to server
    10.                 this.isStillConnect = true;
    11.                 this.isShowMessageWhenDisconnect = true;
    12.                 callback(m);
    13.  
    14.             });
    15.  
    16.         socket.On (SocketIOEventTypes.Disconnect,
    17.             (socket_, originalPacket, args) => {
    18.                 //save that this socket is connecting to server
    19.                 this.isStillConnect = false;
    20.                 Debug.LogError ("socket onDisconnect receive: "+ this.isShowMessageWhenDisconnect);
    21.                 callbackDisConnect(this.isShowMessageWhenDisconnect);
    22.             });
    But I could not catch the error and reconnect_failed events:

    Code (CSharp):
    1. socket.On (SocketIOEventTypes.Error, (socket_, originalPacket, args) => {
    2.             //save that this socket is connecting to server
    3.             Debug.LogError("Connection error with type");
    4.             this.isStillConnect = false;
    5.             DisconnectToServer(true);
    6.         });
    7.  
    8.         socket.On ("error", (socket_, originalPacket, args) => {
    9.             //save that this socket is connecting to server
    10.             Debug.LogError ("Connection error with string");
    11.             this.isStillConnect = false;
    12.            
    13.             DisconnectToServer (false);
    14.         });
    15.  
    16.         socket.On ("reconnect_failed", (socket_, originalPacket, args) => {
    17.             //save that this socket is connecting to server
    18.             Debug.LogError ("reconnect_failed");
    19.             this.isStillConnect = false;
    20.            
    21.             DisconnectToServer (false);
    22.         });
     
  43. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    @BestHTTP

    I have another strange issue that I have come across. I searched the forums and couldn't find anyone with the same issue.

    I am using the socket.io portion and it all works great on WebGL and Android except...

    On Android only, the socket connection disconnects when a new scene is loaded.
    I am using additive scenes with a single persistent scene that is always loaded, that's where all of my network connection code lives. I have verified that those scripts are not reloaded or destroyed/reinitialized when the new scene is added.

    I have added
    Code (CSharp):
    1. BestHTTP.HTTPManager.Logger.Level = BestHTTP.Logger.Loglevels.All;
    To see if the extended logs give me anything, which they do not. Everything seems to go fine, I can connect to my server no problem, I can emit and receive events, but when a new scene is loaded it just closes itself. That is the last log output. "Closed!". The "SocketIOEventTypes.Error" event isn't even fired.

    Again it works fine with loading scenes in WebGL and the editor. And it is not an issue with the way i'm loading scenes, scenes load fine on Android when I am not connected to the socket server, but when I am, it disconnects when I do.

    I am using Unity 5.5.1f1

    Any guidance would be much appreciated, thanks!
     
  44. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @navnahn

    Still no luck to reproduce it, sorry. Is it possible that you can send me repro project?
     
  45. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @Jribeira

    Just tried it on my android device but can't reproduce it.
    The plugin creates only one unity object (HTTPUpdateDelegator) but it should flagged as DontDestroyOnLoad.
    Is it possible to send me the logs, maybe I can see something suspicious.
     
  46. m-garin

    m-garin

    Joined:
    Sep 27, 2015
    Posts:
    4
    Have you solved this problem already? I have a same issue in firefox, but i use Socket.io in WebGl compilation. In Chrome it works perfectly. Plz help :( 2017-03-22.png
     
  47. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @m-garin

    Unfortunately FireFox has a very strict CORS rules, it's very easy to have a working request on Chrome and fail on FireFox.
     
  48. m-garin

    m-garin

    Joined:
    Sep 27, 2015
    Posts:
    4
    This has become clear, but what should I do? If the webGl application does not work in Firefox and Edge - it is useless (
    Maybe you know how to turn on the option withCredentials in the BEST HTTP? I do not know which way to go.
     
  49. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
    @m-garin

    Most of the time the plugin can't do too much, as CORS are handled by the server and the browser.


    You can modify the \Assets\Best HTTP (Pro)\Plugins\WebGL\BestHTTP_WebRequest.jslib add and/or modify withCredentials.
     
  50. Red_Baron_

    Red_Baron_

    Joined:
    Jul 19, 2015
    Posts:
    8
    Hi! I search a solution for HTTP-requests. Does your plugin implement this feature: open HTTP-connection to remote site, send HTTP-request and hold this connection (not disconnect) for next requests (reuse connection)? We have frontend on Unity and backend logic on Apache server, but with ~200 CCU our server drop because too many processes (new process for each request). Thanks!