gpt4 book ai didi

javascript - 如何根据套接字事件呈现新的 jade View ?

转载 作者:行者123 更新时间:2023-11-30 12:04:30 24 4
gpt4 key购买 nike

我有两个主要的 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);
}

我的问题:如何根据套接字事件 startchat 呈现 chatwindow View ?

我看了这个问题: In Express.js, how can I render a Jade partial-view without a "response" object? ,但我不确定如何将它添加到我的代码中,以便在浏览器上加载新的 jade View (聊天窗口)。

最佳答案

您可以使用 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

}
});

关于javascript - 如何根据套接字事件呈现新的 jade View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com