NetEase / UnitySocketIO

socket.io client for unity3d.
889 stars 229 forks source link

error initializing handshake for secure connection #9

Open vrwalking opened 11 years ago

vrwalking commented 11 years ago

when i tried to connect to secure https server "error initializing handshake" connection is given. The server administrator gave the following as javascript method to connect

socket = io.connect('https://abcd.com:321010', {secure: true});

but the in socketio the client constructor takes only one argument, the url.How can we pass this second parameter while connecting? or in general how to connect with secure server??

thanks in advance

halfblood369 commented 11 years ago

The https is secure, so you do not need to add the second argument "{secure: true}".

vrwalking commented 11 years ago

so unitysocketio can be used for secure socket.io communication?

vrwalking commented 11 years ago

one more thing i would like to ask on this thread, i have seen that told already in many issue threads. why socket.emit command is not working. Its working in socketio4net . If its not developed, it would be nice if you can enlighten how should i move to get it worked

vrwalking commented 11 years ago

I have done some digging into the code and after some test i found that, the Event Message is not encoded properly. The code i used is :

IMessage msg = new EventMessage("myevent","{\"jsondata\":\"data\"}",string.Empty,null);

and the msg.Encode shows this:

5:::{"Name":"myevent","Args":["{\"jsondata\":\"data\"}"]}

see the capitalzation of Name and Args, so the server doesn't consider this as an event message. I feel that something similar is causing the socket.On triggering also.

Could you specify which part of the code is handling this,to correct this problem, so that this library is complete, easy client for socketio.

valyard commented 11 years ago

This is because how SImpleJSON works. If a field has name Args it will serialize it to Args. But Socket.io expects args in low case. The same thing happens when data is sent from Socket.io. I don't know how this hasn't been found before because the code doesn't work o.O

Had to add this line for now: evtMsg.MessageText = evtMsg.MessageText.Replace("name", "Name").Replace("args", "Args");

vrwalking commented 11 years ago

thank you for the reply valyard, i had done this solution already and worked but that change is not enough for emit and socket.on to work.The change has to be done to Messages/Helpers/jsonEncodedEventMessage.cs, to two functions that serialize and deserialize the data.We have to change incoming data to capitalized Name and Args for the proper event firing.

public string ToJsonString()
        {
            //return SimpleJson.SimpleJson.SerializeObject(this);
            string jsonString =  SimpleJson.SimpleJson.SerializeObject(this);
            jsonString = jsonString.Replace("Name","name").Replace("Args","args");
            return jsonString;
        }

        public static JsonEncodedEventMessage Deserialize(string jsonString)
        {
            jsonString=jsonString.Replace("name","Name").Replace("args","Args");
            JsonEncodedEventMessage msg = null;
            try { msg = SimpleJson.SimpleJson.DeserializeObject<JsonEncodedEventMessage>(jsonString); }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
            return msg;
        }

Another thing i am encountering is that i am unable to connect to secure servers. On deep investigation the error is unsupported hashing algorithm/encryption, in unity. Can that be a unity mono issue, since it is working nicely on socketio4net.

valyard commented 11 years ago

This isn't a good solution since you can have other occurances of "Name" and "Args" in your data (8

I'm strugling with windows version which doesn't work for some reason )8 The same code on OS X works. And I know nothing about secure servers and hashing methods used in this case.

vrwalking commented 11 years ago

ya you are correct it will capitalize any name and args.If you have cross working problem there can be two possiblites:

  1. json framework 2.improper json data coming in i too had some troubles with that.
halfblood369 commented 11 years ago

@vrwalking Yes, socket.emit command is not working now. If you want it work, maybe the case https://github.com/NetEase/pomelo-unityclient/blob/master/pomelo-unityclient/pomelo-unityclient/EventManager.cs help you.

vrwalking commented 11 years ago

thanks halfblood... but is there any way i can get unitysocketio to work with secured servers? as per logs its says something like encryption algorithm not supported or such, when trying to connect to secure servers using unitysocketio.So the handshake is failing.

halfblood369 commented 11 years ago

Did you have a try to use https to connect the secured servers? And your secured servers must support https service.

vrwalking commented 11 years ago

Yes i tried to connect to a server running https service and socket.io and handshake failed. Have you had successful connection with socketio on secured servers? I have tried unitysocketio's parent socketio4net , in visual studio and it connected successfully...

halfblood369 commented 11 years ago

Thanks for your time, i will have a try to check it out.

eonyanov commented 10 years ago

Hi! I can't connect to sails.js server from Unity3d client. http://stackoverflow.com/questions/25665630/unity3d-unitysocketio-sails-js-error-initializing-handshake Can you help me?