Search Unity

Best HTTP Released

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

  1. pachash

    pachash

    Joined:
    Apr 2, 2014
    Posts:
    55
    @BestHTTP

    By the way it would be very nice if it were possible to set output Stream for the http response. Yep, I know there's a streaming feature via callback to which a list of byte[] is passed. However writing directly to the output Stream would require less memory allocations.
     
  2. BestHTTP

    BestHTTP

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

    I can't and I don't want to defend the current streaming API. There are some API decisions that I regret, and it's one of them.
     
  3. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    76
    Hello! I have an issue again))) When i connected to server (my server on PHP websokets) server got array of Resource ID array{
    [0] = Resource id #6
    [1] = Resource id #7
    [2] = Resource id #8
    [3] = Resource id #9
    }
    and i can disconnect or send private message to some one from the array but if i need disconnect or send msg to certain client i need to know how is how but i don't know))) Could you help me???
     
  4. BestHTTP

    BestHTTP

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

    I'm sorry, but I can't really understand what you want to achieve.
    Are those resource ids are identify other clients? You can send messages to them through the server, but you want to connect to those clients and send messages directly?
     
  5. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    76
    I have the array but for example [0] = Resource id #6 i don't konw how is it and if i want sent message directly to "Alexey"(for example) i don't know [0] = Resource id #6 or [1] = Resource id #7 or [2] = Resource id #8 is he ....how can i identify him???
     
  6. KeeLo

    KeeLo

    Joined:
    Nov 1, 2013
    Posts:
    76
    Ok i try to explain what i want to do... mb i don't understand something please fix me... After client connected to server on the server appears plus one more that ResourceID but i don't know how is it and as i uderstand i can send message via this ResoruceID to client like private msg....and when from client i send message to server for example "who am i?" server answer some message, but this message will receive all connected clients like they are all in one room (broadcast) and i want server to send message directly (private) to client which sent request (like http POST and directly answer)....But if client send with message ResourceID i can do searching in array this ResourceID and answer directly, but client don't know his ResouceID, mb client can get his ID after connected or during handshake i don't know and i ask you )) or mb the server can find out who makes the request, could you help me?
     
    Last edited: Jan 28, 2017
  7. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Does this permit creation of Socket SERVERS within Unity and not just Socket CLIENTS? Also, if yes to this, are simple TCP sockets supported? Thanks!
     
  8. BestHTTP

    BestHTTP

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

    Thank you for the better explanation.
    Well, i think first you should define at least two type of messages that the client can send to the server.
    One is for messages that should be sent to all clients, and one for private messages.

    For private messages the client or the server must know the association between the user name(nickname) and the websocket's resource ID.
    If the client knows about the resource ID, it will send the message to the server with this ID and the actual message. The server must find its websocket handle and send the message to that client.

    (In general though, I don't think disclosing server resources are good idea.)
     
  9. BestHTTP

    BestHTTP

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

    No server capability built into the plugin, sorry.
     
  10. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Hi Tivadar, any updates coming in near future or everything is rock-n-roll for Unity 5.5.1f1?
     
  11. BestHTTP

    BestHTTP

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

    Uploaded an updated yesterday (v1.9.17, you can find its release notes in the first post of this forum), but I don't know any specific issue related to Unity 5.5.1f1 that it should fix. It should work just fine.
    Let me know if you have any issue.
     
    Cromfeli likes this.
  12. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Roger that, will report back how it goes.
     
  13. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
  14. BestHTTP

    BestHTTP

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

    It depends on your server code too. You can upload it encoded in a 'form' as in your link, or as a raw data with additional information in a header.

    Upload it in a form:
    Code (CSharp):
    1. var request = new HTTPRequest(new Uri("http://server.com"), HTTPMethods.Post, onFinished);
    2. request.AddBinaryData("image", texture.EncodeToPNG(), "image.png");
    3. request.Send();
    Upload it as-is:
    Code (CSharp):
    1. var request = new HTTPRequest(new Uri("http://server.com"), HTTPMethods.Post, onFinished);
    2. request.SetHeader("Content-Type", "image/png");
    3. request.Raw = texture.EncodeToPNG();
    4. request.Send();
     
    jGate99 likes this.
  15. baotn

    baotn

    Joined:
    Feb 7, 2017
    Posts:
    2
    Hi, I'm using WebSocket to implement a turnbase game. Whenever ws interrupt then I got an exception and browser was frezze

    exception thrown: TypeError: Cannot read property 'socketImpl' of undefined,TypeError: Cannot read property 'socketImpl' of undefined

    How can I handle this case?
     
  16. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943

    Thank for reply,
    I'm using PHP for file upload and dont know much about backend.

    Question is, HTML upload works, but how C# knows where to put binary data
    http://php.net/manual/en/features.file-upload.post-method.php
     
  17. BestHTTP

    BestHTTP

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

    What browser are you using and what's the version of the plugin?
    Is it possible to give me a better explanation on how I can reproduce this error?
     
  18. BestHTTP

    BestHTTP

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

    In this case you have to use the AddBinaryData function. If you have the html code that you want to convert to a BestHTTP request, I can help.
     
  19. mitchmeyer1

    mitchmeyer1

    Joined:
    Sep 19, 2016
    Posts:
    32
  20. BestHTTP

    BestHTTP

    Joined:
    Sep 11, 2013
    Posts:
    1,664
  21. baotn

    baotn

    Joined:
    Feb 7, 2017
    Posts:
    2
    I'm using v1.9.9 and testing disconnect case. In OnError trigger, I close websocket then this exception thrown: TypeError: Cannot read property 'socketImpl' of undefined,TypeError: Cannot read property 'socketImpl' of undefined. This only happens in WebGL

    Should I check websocket = null then close connection or no need to close websocket?
     
  22. BestHTTP

    BestHTTP

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

    You shouldn't call Close on the websocket in any error handler.
    But I will check it out anyway, thanks!
     
  23. caiopellegrim

    caiopellegrim

    Joined:
    Dec 16, 2016
    Posts:
    4
    Hello! I'm getting this error:
    Error: Request Finished Successfully, but the server sent an error. Status Code: 400- Message: Can "Upgrade" only to "WebSocket".
    UnityEngine.Debug:Log(Object)
    WebsocketC:OnErrorDesc(WebSocket, String) (at Assets/Scripts/WebsocketC.cs:96)
    BestHTTP.WebSocket.WebSocket:OnInternalRequestCallback(HTTPRequest, HTTPResponse) (at Assets/Best HTTP (Pro)/BestHTTP/WebSocket/WebSocket.cs:304)
    BestHTTP.HTTPRequest:CallCallback() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPRequest.cs:1233)
    BestHTTP.ConnectionBase:HandleCallback() (at Assets/Best HTTP (Pro)/BestHTTP/Connections/ConnectionBase.cs:171)
    BestHTTP.HTTPManager:OnUpdate() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPManager.cs:584)
    BestHTTP.HTTPUpdateDelegator:Update() (at Assets/Best HTTP (Pro)/BestHTTP/HTTPUpdateDelegator.cs:166)

    Do you know anything related to it?
    Thanks
     
  24. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
    @BestHTTP

    Getting following error on second scene (and 2nd request) for a simple Get URL which returns result in browser
    Could not find a part of the path "/Users/User/Library/Application Support/DefaultCompany/ProjectName/HTTPCache/3".

    Please advise
     
  25. BestHTTP

    BestHTTP

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

    Is it a persistent problem, or happen only occasionally for the same websocket request?

    While it's true that the plugin send the "Upgrade: websocket" header with a lowercase "websocket", it would be a very strict requirement to allow only upgrades with the "WebSocket" value.
    Even the WebSocket RFC protocol samples are written in lowercase.

    You can edit the \Assets\Best HTTP (Pro)\BestHTTP\WebSocket\WebSocket.cs at line 219 from
    Code (CSharp):
    1. InternalRequest.SetHeader("Upgrade", "websocket");
    to
    Code (CSharp):
    1. InternalRequest.SetHeader("Upgrade", "WebSocket");
     
  26. BestHTTP

    BestHTTP

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

    Are both of your requests are made to the same URL?
     
  27. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
    No, first one is different

    Funny thing is i was able to fix it by disablingcahce=true.
    Which i belive is a temporary fix.


    This problem is occuring when in first scene i get dozens of urls with no issue, but in second scene i get same error (cache is enabled bcz i need caching)


    Please advise :(
     
    Last edited: Feb 9, 2017
  28. BestHTTP

    BestHTTP

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

    Tried to with scene switching, but I wasn't able to reproduce it. Is it possible to set the plugin's log level to all, reproduce it then send the log to me?
    Code (CSharp):
    1. BestHTTP.HTTPManager.Logger.Level = BestHTTP.Logger.Loglevels.All;
     
  29. Degenerative

    Degenerative

    Joined:
    Jun 14, 2015
    Posts:
    2
    Hi, loving the asset so far. I've tried a few and this one blows the rest out of the water.

    I have a, hopefully, simple question. I'm using Socket.IO and I want to get the ID of the current socket. In node I'd normally write "socket.id" or something similar but I cannot find an ID or name property on either the manager or the socket objects.
     
  30. Vascoptorres

    Vascoptorres

    Joined:
    Jul 26, 2012
    Posts:
    18
    @BestHTTP

    Is there any built-in system that handles HTTP requests retries? Basically I want to have all my requests retry to a maximum of 3 times whenever they get a bad response from the server. I can create a wrapper that does this, I just wanted to know if there's something already done.
     
  31. BestHTTP

    BestHTTP

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

    The SocketManager's Handshake object has an Sid property that contains the id of session. The root ("/") namespace's id is the Sid itself.
    For other namespaces you can generate the id with the following formula: 'name of namespace + "#" + Sid'.
     
  32. BestHTTP

    BestHTTP

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

    You can call the .Send() again on the very same request in its callback when you want to resend it.
     
  33. BestHTTP

    BestHTTP

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

    If you have the Pro version of the plugin you can overwrite the old Socket.cs in the \Assets\Best HTTP (Pro)\BestHTTP\SocketIO\ folder by this new one.

    It has a new Id property that should be set properly when connected.
     
  34. Degenerative

    Degenerative

    Joined:
    Jun 14, 2015
    Posts:
    2
    Great to hear, thanks!

    I've got another question for you. I'm trying to send an anonymous object to my socket.io server using the third option from this post (https://forum.unity3d.com/threads/best-http-released.200006/page-10#post-2089958) but it's not hitting the socket.io server.

    I tracked it down to it failing in the JsonMapper.cs class.

    Here is how I'm trying to emit the event.

    Code (CSharp):
    1.      
    2.        var pos = new {
    3.             x = position.x,
    4.             y = position.z
    5.         };
    6.  
    7.         Debug.Log("test -- send move: " + pos);
    8.         // Singleton for manager / socket
    9.         NetworkManager.socket.Emit("move", pos);
    And this is where it seems to be failing with a "General Exception"

    Code (CSharp):
    1.                     PropertyInfo p_info = (PropertyInfo) p_data.Info;
    2.  
    3.                     if (p_info.CanRead) {
    4.                         writer.WritePropertyName (p_data.Info.Name);
    5.                         WriteValue (p_info.GetValue (obj, null),
    6.                                     writer, writer_is_private, depth + 1);
    7.                     }
    My setup is running node version 6.6.0 and socket.io 1.7.2 on windows. Any guidance on this issue?

    Thanks!

    Edit -- Found the cause, there is no Write method for floats. Added one and everything is working fine.

    Thanks for the great asset!
     
    Last edited: Feb 10, 2017
  35. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943

    I'm using following code but i never get success:
    Code (CSharp):
    1.             HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, onRequestFinished);
    2.  
    3. request.AddBinaryData("file", fileBytes, fileNameWithExtension);  
    4.  
    5. //this is where i pass extra parameters
    6. foreach (KeyValuePair<string, string> pair in fields)
    7.                     {
    8.  
    9.                         request.AddField(pair.Key, pair.Value);
    10.                     }
    11.  
    12.             return request.Send();
    13.  

    However here is POSTMAN request which works.

    Please Help :(
     

    Attached Files:

  36. BestHTTP

    BestHTTP

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

    Using this code:
    Code (CSharp):
    1. string url = "http://httpbin.org/post";
    2. Dictionary<string, string> fields = new Dictionary<string, string>();
    3. fields["id"] = "15";
    4. fields["userID"] = "55";
    5. fields["appSecret"] = Guid.NewGuid().ToString();
    6. fields["appID"] = Guid.NewGuid().ToString();
    7.  
    8. byte[] fileBytes = tex.EncodeToJPG();
    9. string fileNameWithExtension = "myfile.jpg";
    10.  
    11. HTTPRequest request = new HTTPRequest(new Uri(url), HTTPMethods.Post, onRequestFinished);
    12.  
    13. request.AddBinaryData("file", fileBytes, fileNameWithExtension, "image/jpeg");
    14.  
    15. //this is where i pass extra parameters
    16. foreach (KeyValuePair<string, string> pair in fields)
    17. {
    18.  
    19.     request.AddField(pair.Key, pair.Value);
    20. }
    21.  
    22. request.Send();
    Received the following result from the server:
    upload_2017-2-14_9-3-22.png

    As you see it contains both the file and fields.

    Please try out Charles as an intermediate proxy, configure postman to use this proxy and send your request. After these steps you can send a screenshot from the raw request so I will know how postman sent the form.
    You can also configure the plugin to use a proxy:
    Code (CSharp):
    1. BestHTTP.HTTPManager.Proxy = new BestHTTP.HTTPProxy(new Uri("http://localhost:8888"), null, true);
    This is how the raw request looked like for the code above:
    upload_2017-2-14_9-8-45.png
     
    jGate99 likes this.
  37. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,943
    Thank you :) It worked.
     
  38. invictvs1

    invictvs1

    Joined:
    Mar 7, 2014
    Posts:
    51
    @BestHTTP

    Hi there, BestHTTP is a phenomenal plugin and have been using it for quite some time without many hiccups. I'm running into a bit of a problem right now though, when I try to stream a download, it seems to break with larger files (~2.5Gb). My Download Streaming callback is called with a null response and I get the request exception of "Value is too large". I know this can occur when a number is too large to be parsed to an int, is there a file size limit on downloading? Is there any way to get around this issue? Thanks in advance!
     
  39. BestHTTP

    BestHTTP

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

    Most probably the server sent a Content-Length header and as the plugin tries to convert&store it in an int it throws an exception.
    I see two ways to fix it:
    1.) If the server sends the content with Chunked Transfer-Encoding instead of raw data it will not contain a Content-Length header.
    2.) Modifying the plugin to use longs instead of ints for these cases. Unfortunately, it would involve public interface changes (download progress reporting), so I would try to avoid it.
     
  40. andermon

    andermon

    Joined:
    Aug 29, 2016
    Posts:
    14
    Hi, I was wondering can one set the version of TLS for ones HTTPRequests? and if so do you have an example.

    We were sending data via a proxy to a server that has recently had its TLS upgraded. No more TLS 1.0. Since then we are getting "Possible TLS attack" errors.

    Thanks for the help
     
  41. BestHTTP

    BestHTTP

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

    What version of the plugin are you using? The default TLS handler should handle higher versions, and do so automatically.
     
  42. andermon

    andermon

    Joined:
    Aug 29, 2016
    Posts:
    14
  43. BestHTTP

    BestHTTP

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

    Hmm, strange. Can you send a link to my email that uses fails?
    In the meantime you can try to use an another TLS handler by flipping a flag:
    Code (CSharp):
    1. BestHTTP.HTTPManager.UseAlternateSSLDefaultValue = false;
     
  44. hoyeon-c

    hoyeon-c

    Joined:
    Jul 8, 2015
    Posts:
    4
    I am using websocket.
    "OnClosed" or "onError" is not called when Internet line is disconnected after connection.
    At this time, WebSocket.Close () does not break even.
    Also, when reconnecting, no data is received after the connection is completed.
     
  45. invictvs1

    invictvs1

    Joined:
    Mar 7, 2014
    Posts:
    51
    Appreciate the insight @BestHTTP - the problem is exactly as you described. Unfortunately, I need to read the Content-Length header prior to downloading the file, so I think I have to modify the plugin's code itself.
     
    Last edited: Feb 17, 2017
  46. BestHTTP

    BestHTTP

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

    It's a known limitation of the plugin, but the problem lays deeper.
    TCP connections could send keep-alive packets to detect disconnections, but on windows it's around 24 hours if I remember right. I tried to support a lower timing, but it turned out very unreliable and there are differences between OSs too.
    I kept my attempts in the source code, you can try it out by uncommenting lines in the \Assets\Best HTTP (Pro)\BestHTTP\PlatformSupport\TcpClient\TcpClient.cs from lines 467 through 480.
    There are some references too for further reading.
     
  47. BestHTTP

    BestHTTP

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

    I think you have to modify only two files: HTTPRequest.cs and HTTPResponse.cs. I advice to start with HTTPResponse.cs @ line 312: ReadRaw(Stream, int.Parse(contentLengthHeaders[0])); should be
    ReadRaw(Stream, long.Parse(contentLengthHeaders[0]));
    You have to modify the ReadRaw's signature, then you can follow the path of compile errors. ;)
     
  48. andermon

    andermon

    Joined:
    Aug 29, 2016
    Posts:
    14
    @BestHTTP

    Thanks for the help, we narrowed down the problem, it is not due to the TLS, it is a proxy issue, as we have a proxy which relies on user credentials from when the user logs into the machine. Can BestHTTP auto-detect default proxy settings and credentials that IE(system settings) is using?

    As always thank you for your speedy responses :)
     
  49. BestHTTP

    BestHTTP

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

    Unfortunately, no. The plugin can't detect system settings. I done some experiment with it on Windows, but my tests gave unreliable results so I gave it up.
     
  50. andermon

    andermon

    Joined:
    Aug 29, 2016
    Posts:
    14
    Thanks for the help, I have been looking into Kerberos, and trying to pass the windows token login token on, but I think it all comes down to SSO/SAML. Will keep looking into it. Thanks for all the help and the AWESOME plugin