我正在尝试使用SockJS为我的wordpress网站制作聊天应用程序。我的问题是客户端连接到服务器后断开连接。我不知道。 这是我的服务器和客户端代码。
服务器:
var fetchedResultsController: NSFetchedResultsController<Content>!
我的客户代码:
var http = require('https');
var sockjs = require('sockjs');
var fs = require("fs");
var commands = {
key: fs.readFileSync('privkey.pem'),
cert: fs.readFileSync('cert.pem')
};
var clients = [];
var users = [];
var messages = [];
function broadcast(message){
for (var client in clients){
clients[client].write(JSON.stringify(message));
}
}
var echo = sockjs.createServer();
echo.on('connection', function(conn) {
clients[conn.id] = conn;
conn.on('data', function(e) {
var data = JSON.parse(e);
switch(data.action){
case "new_user":
clients[conn.id]["username"]=data.username;
users[data.username]["avatar"]=data.avatar;
users[data.username]["url"]=data.url;
case "new_message":
data.avatar=users[data.username]["avatar"];
data.url=users[data.username]["url"];
messages.unshift(data);
if(messages.length>100){
messages.length=100;
}
broadcast(data);
}
});
conn.on('close', function() {
delete clients[conn.id];
delete users[clients[conn.id]["username"]];
});
});
var server = http.createServer(commands);
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');
控制台输出如下:
chat.js?ver = 4.5.3:66连接到聊天服务器
chat.js?ver = 4.5.3:71与聊天服务器断开连接
chat.js?ver = 4.5.3:66连接到聊天服务器
chat.js?ver = 4.5.3:71与聊天服务器断开连接
有什么问题? 感谢
答案 0 :(得分:1)
If you use forever/pm2/smth else then I think problem on server-side
users[data.username]["url"]=data.url;
break; // missed!
case "new_message":
答案 1 :(得分:0)
问题是服务器文件上的简单对象语法错误。