简体   繁体   中英

How do I render a new jade view upon a socket event?

I have two major js files, one on the server side which is the server.js and another on the client side, which is enterchat.js . These two files are the ones which will communicate via socket.io. All socket events are working as expected.

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

My question: How do I render the chatwindow view upon the socket event startchat ?

I looked at this question: In Express.js, how can I render a Jade partial-view without a "response" object? , but I am not sure as to how to add it in my code so that a fresh jade view (chatwindow) is loaded on the browser.

You can use compileFile method of jade api, get the html and then emit a socket event containing the html data. You can append that html to the 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

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM