Totaljs FrameworkWebSocket客户端

时间:2015-07-02 00:35:42

标签: javascript node.js total.js

我想使用websockets将更新从服务器发送到客户端。

我知道我可以使用Server Sent Events但是Internet Explorer与它没有很好的兼容性所以我更喜欢使用websocket。

TotalJs WebSocket允许我像客户一样使用它吗?

我正在尝试这样做:(TotalJs Websocket示例)

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);
    });
}

当有人连接到http://127.0.0.1/send时,连接到服务器的所有客户端都会收到一条消息。

Node.js没有本机WebSocket支持,因此send_message()函数不起作用。我想使用TotalJs Websocket支持,但我不知道如何像客户端一样使用它。

多数民众赞成。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我知道这不是最好的方法,但我使用全局变量来解决。

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);
    });
}