sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.72k stars 1.66k forks source link

Unity 4.3 can't connect to websocket #44

Open GlenDC opened 10 years ago

GlenDC commented 10 years ago

Hi!

I have a C# Console application that serves as my server. My unity game serves as the client ( and has your plugin embedded ).

However it seems like I never can connect to my server.

I tried your code you posted @ Issue #35 and I get the following output:

Open Socket: CONNECTING Websocket Alive: False WebSocket-> Error: An exception has occured while connecting. Open socket-> OnError: CONNECTING WebSocket-> Close-code: 1006 Websocket-> Close-reason: An exception has occured while connecting Open Socket -> OnClose: CLOSED WebSocket-> Error: The WebSocket connection has already been closed. Open Socket -> OnError: CLOSED WebSocket-> Error: The WebSocket connection has already been closed. . . . Open Socket -> OnError: CLOSED WebSocket-> Error: The WebSocket connection has already been closed. . . .

Any idea what the problem is? Note that when i make another C# console application as a client, and have similar code in it, that it does work. So it only doesn't work when using it from within unity...

GlenDC commented 10 years ago

When i make my Unity game a server, and my console application the client, than it does work. Perhaps this helps you more in what's wrong.

sta commented 10 years ago

Hello there,

Which Build Settings (e.g. PC and Mac Standalone) did you use as a client in Unity Editor?

And your websocket-sharp.dll is self build?

GlenDC commented 10 years ago

No Build, it was all within Unity Editor itself. In the end I will use a PC Standalone, but for now I don't need it.

The websocket-sharp.dll used by my console application is self build, however the one in unity is the one that came with the Unity Plugin.

sta commented 10 years ago

No Build, it was all within Unity Editor itself.

Hmm,,, do you mean your websocket-sharp.dll in Unity Editor came from Unity Asset Store?

In the end I will use a PC Standalone, but for now I don't need it.

What do you mean? Does it mean that you select other platform in Build Settings in Unity Editor?

GlenDC commented 10 years ago

1) Yes the websocket-sharp.dll used in Unity Editor, comes from the Unity Asset Store. 2) I mean that I'm just playing the game from within my editor (By pressing the play button).

PS: I also posted a comment on issue #35, could you also take a look at that one, as that one is more annoying than this one.

sta commented 10 years ago

Could you describe your selected platform? (Plz take a look at window title bar of your Unity Editor. (e.g. 'Unity - Untitled - your project name - PC, Mac & Linux Standalone' <- here))

And which version of Unity (Free or Pro) do you use?

GlenDC commented 10 years ago

Selected platform is PC, Mac & Linux Standalone. With as only goal to release on Windows, as it is just a prototype project for an upcomming project.

I'm using the Pro version of Unity.

sta commented 10 years ago

Thanks for input.

    void Start () {

        ws = new WebSocket ("ws://localhost:8080"); // <- This url is correct as your server url (or port)?
        ...
    }

And could you try Echo Back Test in Unity Editor (select Window > WebSocket-Sharp > Echo Back Test on window menu bar in Unity Editor)?

GlenDC commented 10 years ago
sta commented 10 years ago

I did and I get the following error message...

Hmm,,, it's weird. Could you make new project again with PC, Mac & Linux Standalone platform, and test it?

And could you try following?

    void Start () {

        ws = new WebSocket (your_server_url);
        ws.Log.Level = LogLevel.Trace;
        ws.Log.File = path_to_your_log_file;
        ...
    }
GlenDC commented 10 years ago
 ws.Log.Level = LogLevel.Trace

Can't start that code because of this error:

'WebSocketSharp.LogLevel' does not contain a definition for 'Trace'.

Can you btw really take a look at the other issue where I posted, as that one is quite urgent for my project...

sta commented 10 years ago

Ahh, LogLevel.TRACE is correct for your websocket-sharp.

ws.Log.Level = LogLevel.TRACE

Can you btw really take a look at the other issue where I posted, as that one is quite urgent for my project...

Could you replace OnDestroy() with OnApplicationQuit()?

    void OnApplicationQuit() {
        if (ws != null && ws.ReadyState == WebSocketState.Open)
            ws.Close ();
    }

And

I can start the game, and all works fine.

What does it mean? You can connect to your server?

GlenDC commented 10 years ago

Could you replace OnDestroy() with OnApplicationQuit()?

That did the trick, thank you, that problem is now solved for me! :+1:

What does it mean? You can connect to your server?

What I meant was that there was no problem, untill I would close the application, than it would crash. But as I just said, it is now solved, so that's fine!