我正在尝试使用node .js构建一个聊天系统,这是一个非常基本的项目,但是我无法使其正常工作。我有一个app.js文件(基本上是我的服务器文件),一个用于管理index.html请求的client.js文件和一个应由消息传递系统负责的chatroomManager.js文件。
app.js文件
<things>
<code V="123"/>
<region V="38"/>
<hl_list>
<hl>
<stuff_hmv V="XXX" DN="some other stuff"/>
<secondary_code V="Z98"/>
<infotext V="max 12 units"/>
<kl_list>
<kl V="I" DN="some other stuff">
<diag_list>
<diag V="WS1" DN="illness 1"/>
<diag V="WS2" DN="illness 2"/>
<diag V="EX1" DN="illness 3"/>
</diag_list>
</kl>
</kl_list>
</hl>
</hl_list>
</things>
<things>
<code V="456"/>
<region V="45"/>
<hl_list>
<hl>
<stuff_hmv V="X1X" DN="some other stuff"/>
<infotext V="max 13 units"/>
<kl_list>
<kl V="II" DN="some other stuff">
<diag_list>
<diag V="WS1" DN="illness 3"/>
<diag V="WS2" DN="illness 2"/>
<diag V="EX1" DN="illness 4"/>
</diag_list>
</kl>
</kl_list>
</hl>
</hl_list>
</things>
client.js文件
const User = require('./public/scripts/User');
const express = require("express");
const app = express();
var server = require('http').createServer(app);
var io=require('socket.io')(server);
var bodyParser = require('body-parser')
var fs = require("fs");
var userlist= new Array();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/views/index.html');
});
app.get('/:roomname',function (req,res) {
res.sendFile(__dirname + '/public/views/chatroom.html', {name:req.params.roomname});
});
io.on('connection', function(socket){
//Leggiamo le rooms attuali
var contents = fs.readFileSync("public/scripts/rooms.json");
let rooms = JSON.parse(contents);
socket.emit('parse',rooms);
//un utente prova a connettersi
socket.on('connection tried', function (name,room) {
if(!find(name)){ //see if username is available
const user = new User(name, room," ");
userlist.push(user); //insert user in userlist
socket.join(room); //user joins the room
socket.broadcast.to(room).emit('room-entrance',room,user); //telling the room that user is in
socket.emit('redirect', room, user); //user joins the room
}
else
socket.emit('RegistrationErr','username'); //in case username isn't available error is logged
});
socket.on('user log',function (user,room) {
console.log(user, room);
socket.to(room).emit('welcome');
});
});
chatroomManager.js文件
$(function () {
var socket = io()
socket.on('parse',function (rooms) {
options(rooms);
});
$('#formElem').submit(function (e) {
e.preventDefault();
let roomSel=document.getElementById('room');
socket.emit('connection tried', $('#name').val(), roomSel.options[roomSel.selectedIndex].value);
return false;
});
socket.on('RegistrationErr', function (error) { /* IMPLEMENT THIS*/
console.log(error)
});
socket.on('redirect',function (room, user) {
//questo evento viene emesso per il chatroom manager che sa che un utente è entrato
window.location.href='/'+room;
})
});
基本上,最后,“欢迎”应该在控制台上记录,但是什么都没有记录。我该如何解决?