如何在套接字事件上呈现新的玉视图?

时间:2016-02-24 15:03:29

标签: javascript node.js express socket.io pug

我有两个主要的js文件,一个在服务器端,server.js,另一个在客户端,enterchat.js。这两个文件是通过socket.io进行通信的文件。所有套接字事件都按预期工作。

server.js

var express = require('express'),
...
server = require('http').createServer(app),
io = require('socket.io').listen(server);

var usernames = [],
    username_sockets = [];

...

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use('/', express.static(__dirname+'/public/'));

app.get('/chat', function (req, res) {
    res.render('checkUsername', {title:'Socket IO Chat'});
});

app.get('/chatwindow', function (req, res) {
    res.render('chatwindow', {title:'Welcome to chat window'});
});

io.sockets.on('connection', function (socket) {

    socket.on('disconnect', function () {
        ...
        delete username_sockets[socket.id];
        console.log("Disconnected from " + user);
    });

    socket.on('newusr', function (newusrname) {
        console.log("New user name request:: " + newusrname);
        if(usernames.indexOf(newusrname) >= 0)
        {
            console.log("Already used username..");
            socket.emit('usernameTaken', newusrname);
        }
        else
        {
            socket.emit('usernameavlbl', newusrname);
        }
    });

    socket.on('startchat', function (usernameAvailable) {
        if(usernames.indexOf(usernameAvailable) >= 0)
        {
            console.log("Just taken username..");
            socket.emit('usernameJustTaken', usernameAvailable);    //returning the username that was just taken
        }
        else
        {
            usernames.push(usernameAvailable);
            console.log("Opening chat window for "+usernameAvailable);
            username_sockets[socket.id] = usernameAvailable;

            //  trying to render jade view to open chatwindow on socket event
        }
    });

    socket.on('sndmsg', function (message) {
        socket.broadcast.emit('msgreceive', message, username_sockets[socket.id]);
    });

    socket.on('typing', function (username) {
        socket.broadcast.emit('usertyping', username);
    });

    socket.on('stoppedtyping', function (username) {
        socket.broadcast.emit('userstoppedtyping', username);
    });
});

server.listen(8080,'0.0.0.0');
console.log("Listening on 8080..");

enterchat.js

var socket, usernameAvailable;

    $(document).ready(function () {
        connect();

    ...

    ...

    $('#checkBtn').on('click', function(event) {
        if($('#username').val() == '')
            alert("Choose a username");
        else
        {
            var newusrname = $('#username').val();
            socket.emit('newusr', newusrname); 
        }
    });

    ...

    socket.on('usernameTaken', function (message) {
        alert(message + " is already taken. Try another one..");
    });

    socket.on('usernameJustTaken', function (message) {
        alert(message + " was just taken. Try another one..");
    });

    socket.on('usernameavlbl', function (newusrname) {
        $('#chataway').attr('disabled', false);
        usernameAvailable = newusrname;
    });

    $('#chataway').on('click', function () {
        socket.emit('startchat', usernameAvailable);
    });
    });

    function connect () {
        socket = io.connect(null);
    }

我的问题:如何在套接字事件chatwindow上呈现startchat视图?

我查看了这个问题: answer ,但我不确定如何将其添加到我的代码中,以便在浏览器中加载新的玉视图(chatwindow)

1 个答案:

答案 0 :(得分:3)

您可以使用jade api的compileFile方法,获取html,然后发出包含html数据的套接字事件。您可以将该html附加到DOM。

socket.on('startchat', function (usernameAvailable) {
    if(usernames.indexOf(usernameAvailable) >= 0)
    {
        console.log("Just taken username..");
        socket.emit('usernameJustTaken', usernameAvailable);    //returning the username that was just taken
    }
    else
    {
        usernames.push(usernameAvailable);
        console.log("Opening chat window for "+usernameAvailable);
        username_sockets[socket.id] = usernameAvailable;

        var fn = jade.compileFile('path to jade file', options);

        // Render function
        var html = fn();

        // Now you can send this html to the client by emitting a socket event

    }
});