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

Doesn't work if I create Http server on same host but with other port #34

Open
CrazyFlasher opened this issue Sep 12, 2021 · 6 comments

Comments

@CrazyFlasher
Copy link

CrazyFlasher commented Sep 12, 2021

Howerver works if I create websocket server before creating http server.

@ianharrigan
Copy link
Owner

Hmmm - do you have a minimal example to run? Sounds strange.

@CrazyFlasher
Copy link
Author

Doesn't work

import haxe.io.Bytes;
import hx.ws.Log;
import hx.ws.SocketImpl;
import hx.ws.Types.MessageType;
import hx.ws.WebSocket;
import hx.ws.WebSocketHandler;
import hx.ws.WebSocketServer;
import js.node.Http;

class Ws
{
    static public function main()
    {
        #if js
        untyped global.WebSocket = require("ws");
        #end

        var server:js.node.http.Server = Http.createServer();

        server.listen(5000, "localhost", () -> {
            trace("Http server created");

            new Server();
            new Client();
        });
    }
}

class Client {
    public function new() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var ws = new WebSocket("ws://localhost:5001");
        ws.onopen = function() {
            ws.send("alice string");
            ws.send(Bytes.ofString("alice bytes"));
        }
    }
}

class Server {
    public function new() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var server = new WebSocketServer<Handler>("localhost", 5001, 10);
        server.start();
    }
}

class Handler extends WebSocketHandler {
    public function new(s: SocketImpl) {
        super(s);
        onopen = function() {
            trace(id + ". OPEN");
        }
        onclose = function() {
            trace(id + ". CLOSE");
        }
        onmessage = function(message: MessageType) {
            switch (message) {
                case BytesMessage(content):
                    trace(content.readAllAvailableBytes());
                case StrMessage(content):
                    var str = "echo: " + content;
                    trace(str);
                    send(str);
            }
        }
        onerror = function(error) {
            trace(id + ". ERROR: " + error);
        }
    }
}

Works

import haxe.io.Bytes;
import hx.ws.Log;
import hx.ws.SocketImpl;
import hx.ws.Types.MessageType;
import hx.ws.WebSocket;
import hx.ws.WebSocketHandler;
import hx.ws.WebSocketServer;
import js.node.Http;

class Ws
{
    static public function main()
    {
        #if js
        untyped global.WebSocket = require("ws");
        #end

        var server:js.node.http.Server = Http.createServer();

        new Server();
        new Client();

        server.listen(5000, "localhost", () -> {
            trace("Http server created");
        });
    }
}

class Client {
    public function new() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var ws = new WebSocket("ws://localhost:5001");
        ws.onopen = function() {
            ws.send("alice string");
            ws.send(Bytes.ofString("alice bytes"));
        }
    }
}

class Server {
    public function new() {
        Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
        var server = new WebSocketServer<Handler>("localhost", 5001, 10);
        server.start();
    }
}

class Handler extends WebSocketHandler {
    public function new(s: SocketImpl) {
        super(s);
        onopen = function() {
            trace(id + ". OPEN");
        }
        onclose = function() {
            trace(id + ". CLOSE");
        }
        onmessage = function(message: MessageType) {
            switch (message) {
                case BytesMessage(content):
                    trace(content.readAllAvailableBytes());
                case StrMessage(content):
                    var str = "echo: " + content;
                    trace(str);
                    send(str);
            }
        }
        onerror = function(error) {
            trace(id + ". ERROR: " + error);
        }
    }
}

Build command

haxe -js ws.js -main Ws -debug -cp src -lib hxWebSockets -lib hxnodejs

Run command

node ws.js

@ianharrigan
Copy link
Owner

So i wonder if a better #if is #if nodejs as using just js im pretty sure will break normal, browser js builds.

@CrazyFlasher
Copy link
Author

Ok. But the topic of question is another :)

@ianharrigan
Copy link
Owner

Oh, sorry, got mixed up!

@CrazyFlasher
Copy link
Author

Issue is in MainLoop for NodeJS target in this case.
I've changed tick logic for NodeJS to haxe.Timer and works fine for me now.
Created pull request:
#35

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

2 participants