Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity 4.3.1f1 and websocket-sharp freeze Unity Editor. #35

Open
mrakob opened this issue Mar 27, 2014 · 10 comments
Open

Unity 4.3.1f1 and websocket-sharp freeze Unity Editor. #35

mrakob opened this issue Mar 27, 2014 · 10 comments

Comments

@mrakob
Copy link

mrakob commented Mar 27, 2014

Self build websocket-sharp, and add dll (Assets/Plugins). And Unity Editor freeze.

using UnityEngine;
using System.Collections;

using System;
using System.Threading;
using WebSocketSharp;

public class NewBehaviourScript : MonoBehaviour {

void Start () {

    using (WebSocket ws = new WebSocket ("ws://localhost:8080")) {

        print ("Open socket: "+ ws.ReadyState);

        print ("Websocket Alive: "+ ws.IsAlive);


        ws.OnMessage += (sender, e) => {
            print("From srv:" + e.Data);
        };

        ws.OnOpen += (sender, e) => {

            print("WebSocket-> Open:");

            print ("Open socket-> OnOpen: "+ ws.ReadyState);

        };
        ws.OnError += (sender, e) => {
            print("WebSocket-> Error:"+ e.Message);

            print ("Open socket-> OnError: "+ ws.ReadyState);
        };

        ws.OnClose += (sender, e) => {
            print("WebSocket-> Close-code:"+ e.Code);
            print("WebSocket-> Close-reason:"+ e.Reason);

            print ("Open socket-> OnClose: "+ ws.ReadyState);
        };

        ws.Connect ();
    }

}

@sta
Copy link
Owner

sta commented Mar 28, 2014

Could you try the following?

using UnityEngine;
using System.Collections;

using System;
using System.Threading;
using WebSocketSharp;

public class NewBehaviourScript : MonoBehaviour {

    WebSocket ws;

    void Start () {

        ws = new WebSocket ("ws://localhost:8080");

        print ("Open socket: " + ws.ReadyState);

        print ("Websocket Alive: " + ws.IsAlive);

        ws.OnMessage += (sender, e) => {
            print ("From srv: " + e.Data);
        };

        ws.OnOpen += (sender, e) => {
            print ("WebSocket-> Open:");

            print ("Open socket-> OnOpen: " + ws.ReadyState);
        };

        ws.OnError += (sender, e) => {
            print ("WebSocket-> Error: " + e.Message);

            print ("Open socket-> OnError: " + ws.ReadyState);
        };

        ws.OnClose += (sender, e) => {
            print ("WebSocket-> Close-code: " + e.Code);
            print ("WebSocket-> Close-reason: " + e.Reason);

            print ("Open socket-> OnClose: " + ws.ReadyState);
        };

        ws.Connect ();
    }

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

And, is the server running?

@mrakob
Copy link
Author

mrakob commented Mar 28, 2014

Copy & paste your code, and copy websocket-sharp.dll to Assets/Plugins. Run Unity Editor, and editor freeze.

Kill process Unity editor, and delete websocket-sharp.dll from Assets/Plugins. Run Unity editor, not freeze, err message:

Assets/NewBehaviourScript.cs(8,7): error CS0246: The type or namespace name `WebSocketSharp' could not be found. Are you missing a using directive or an assembly reference?

And, is the server running?

server not running.

add: run server, node.js


var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({port: 8080});

wss.on('connection', function(ws) {

    console.log("connection" + ws);


    ws.on('message', function(message) {
        console.log('received: %s', message);

        ws.send("text");



        /*
        setInterval(function() {
            ws.send(Date.now().toString(), {mask: true});
            console.log('setTimeout send');

        }, 500);
        */

    });

    ws.on('error', function(e) {

        console.log('error1:');
        console.log(e);

    });

    ws.on('close', function() {
        console.log('disconnected');

    });

});

wss.on('error', function(e){
    console.log('error:');
    console.log(e);
});

and run Unity editor. Copy websocket-sharp.dll, and run project. Unity freeze.
srv terminal print:
connection[object Object]

@sta
Copy link
Owner

sta commented Mar 28, 2014

Hmm,,,

Copy & paste your code, and copy websocket-sharp.dll to Assets/Plugins. Run Unity Editor...

Do you mean 'Copy & paste' has been done without Unity Editor?

I don't know your situation correctly, so could you try the following steps?

  1. Run Unity editor.
  2. Create new project in Unity editor.
  3. Create 'Plugins' folder under 'Assets' of that project in Unity editor.
  4. Copy & paste (or drag & drop) websocket-sharp.dll to that 'Plugins' folder in Unity editor.

Do you get something error so far?

@mrakob
Copy link
Author

mrakob commented Mar 31, 2014

problem disappeared
thx

@GlenDC
Copy link

GlenDC commented May 12, 2014

I can start the game, and all works fine.
Than I can stop the game, and all is still fine.

But than when I start again, Unity crashes.
Any idea on why this is?

@nfmelendez
Copy link

Use method Awake instead of Start

@Marreck
Copy link

Marreck commented Jan 13, 2015

I have a problem which might be related.

In the Unity editor when the server I want to connect to is down, and I stop the player while trying to connect and then start the player again the editor will freeze.

However, when I stop the player (while trying to connect) and wait till the connect request is timed out (which I have modified to be just 10 seconds so I don't have to wait the whole default 90secs) the editor will not freeze when I start it again.

I am using a self build websocket dll. My code:

void Awake() {
 ws = new WebSocket (Server);
 s.OnMessage += onMessage;
 ws.OnOpen += onConnect;
 ws.OnError += onError;
 ws.OnClose += onClose;
 ws.ConnectAsync ();
}
void OnApplicationQuit() {
 ws.OnMessage -= onMessage;
 ws.OnOpen -= onConnect;
 ws.OnError -= onError;
 ws.OnClose -= onClose;
 if (ws != null && ws.ReadyState == WebSocketState.Open) ws.CloseAsync ();
}

When I don't remove the event handlers they still fire even though I already have stopped the editor so it seems to me that the websocket process is still running after I have stopped the Unity editor.

How can I force Unity to kill the process when it is still trying to connect to the server?

Like I said, not a big problem since the server should always be running anyways and 10 seconds isn't that long to wait, but I have the feeling that I am missing something here.

@sta
Copy link
Owner

sta commented Jan 15, 2015

@Marreck Hello there,

To replace with the following, in the OnApplicationQuit method:

void OnApplicationQuit() {
 ...
 if (ws != null) ws.Close ();
}

Does the above make a difference? (It might make you wait.)

I guess to use that async close method is not good for your case.

@Marreck
Copy link

Marreck commented Jan 15, 2015

Hi,

Thanks for your help! With that modification when I pause the editor, it hangs until the connect attempt times out (In my case max 10 seconds instead of the default 90 seconds as set in WebSocket.cs). This kinda solves this problem since you can't press play while the editor hangs and thus can't crash by attempting to reconnect while a previous connection attempt is still happening :).

It seems a bit of a weird solution though. Isn't there a way to abort any ongoing connection attempts?

@JadeWhite
Copy link

I'm working on a separate library and I'm having the same issue. Has anyone managed to find a way to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants