0

I would like to use websockets to send updates from the server to the clients.

I know I can use Server Sent Events but Internet Explorer doesn't have great compatibility with it so I prefer to use websocket.

The TotalJs WebSocket allow me to use it like a client?

I'm trying to do this: (TotalJs Websocket Example)

exports.install = function(framework) {
    framework.route('/');
    framework.route('/send/', send_message);

    framework.websocket('/', socket_homepage, ['json']);
};

function send_message() {
    var controller = this;
    var socket = new WebSocket('ws://127.0.0.1:8000/');
    socket.onopen = function() {
        socket.send(encodeURIComponent(JSON.stringify({ message: "send_message" })));
    };
    socket.close();
    socket = null;
}

function socket_homepage() {
    var controller = this;

    controller.on('open', function(client) {
        console.log('Connect / Online:', controller.online);

        client.send({ message: 'Hello {0}'.format(client.id) });
        controller.send({ message: 'Connect new user: {0}\nOnline: {1}'.format(client.id, controller.online) }, [], [client.id]);
    });

    controller.on('close', function(client) {
        console.log('Disconnect / Online:', controller.online);
        controller.send({ message: 'Disconnect user: {0}\nOnline: {1}'.format(client.id, controller.online) });
    });

    controller.on('message', function(client, message) {
        console.log(message);

        if (typeof(message.username) !== 'undefined') {
            var old = client.id;
            client.id = message.username;
            controller.send({ message: 'rename: ' + old + ', new: ' + client.id });
            return;
        }

        // send to all without this client
        message.message = client.id + ': ' + message.message;
        console.log(message);
        controller.send(message);
    });
}

When someone connect to http://127.0.0.1/send all clients connected to the server will receive a message.

Node.js doesn't have native WebSocket support so the send_message() function does not work. I would like to use TotalJs Websocket support but I don't know how to use it like a client.

Thats it.

Thank you very much.

Victor Santos
  • 350
  • 1
  • 4
  • 14
  • I'm not familiar with total.js but have you seen the example in the docs: http://docs.totaljs.com/v1.8.x/en.html#api~FrameworkController~controller.send – Jörn Jul 02 '15 at 00:41
  • Tried now but I think that controller.send() only exists if we route using "framework.websocket" and I'm using "framework.route". I tried "console.log(controller.send);" inside send_message() function and I received "undefined". Thank you very much. – Victor Santos Jul 02 '15 at 01:20

1 Answers1

0

I know this is the not best way but I used a global variable to resolve.

exports.install = function(framework) {
    framework.route('/');
    framework.route('/send/', send_message);

    framework.websocket('/', socket_homepage, ['json']);
};

function send_message() {
    if (websocket == null) return;
    websocket.send({ message: "Hello World!" });
}

var websocket = null;
function socket_homepage() {
    //var controller = this;
    websocket = this;

    websocket.on('open', function(client) {
        console.log('Connect / Online:', websocket.online);

        client.send({ message: 'Hello {0}'.format(client.id) });
        websocket.send({ message: 'Connect new user: {0}\nOnline: {1}'.format(client.id, websocket.online) }, [], [client.id]);
    });

    websocket.on('close', function(client) {
        console.log('Disconnect / Online:', websocket.online);
        websocket.send({ message: 'Disconnect user: {0}\nOnline: {1}'.format(client.id, websocket.online) });
    });

    websocket.on('message', function(client, message) {
        console.log(message);

        if (typeof(message.username) !== 'undefined') {
            var old = client.id;
            client.id = message.username;
            websocket.send({ message: 'rename: ' + old + ', new: ' + client.id });
            return;
        }

        // send to all without this client
        message.message = client.id + ': ' + message.message;
        console.log(message);
        websocket.send(message);
    });
}
Victor Santos
  • 350
  • 1
  • 4
  • 14
  • I'm in a situation where I need to send the message through the socket on the different server? Do you have any idea? function send_message() { var controller = this; var socket = new WebSocket('ws://127.0.0.2:8000/'); socket.onopen = function() { socket.send(encodeURIComponent(JSON.stringify({ message: "send_message" }))); }; socket.close(); socket = null; } Can I open a new server socket like this in node.js? – Code_Crash Jan 02 '18 at 09:38