连接后 SockJS 断开连接
SockJS disconnects after connect
我正在尝试使用 SockJS 为我的 wordpress 网站制作聊天应用程序。我的问题是客户端在连接到服务器后断开连接。我不知道。
这是我的服务器和客户端代码。
服务器:
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');
我的客户代码:
var recInterval = null;
var socket = null;
console.log("loaded js");
function new_user(){
// Get the content from the textbox
// The object to send
var send = {
action: "new_user",
username: user.username,
avatar: user.avatar,
url: user.url
};
// Send it now
socket.send(JSON.stringify(send));
}
function send_message(message){
// Get the content from the textbox
// The object to send
var send = {
action: "new_message",
username: user.username,
message: message
};
// Send it now
socket.send(JSON.stringify(send));
}
function new_conn(){
socket = new SockJS('https://example.com:9999/echo');
clearInterval(recInterval);
socket.onopen = function() {
console.log('connected to chat server');
connected=true;
new_user();
};
socket.onclose = function() {
console.log('disconnected from chat server');
socket = null;
recInterval = setInterval(function() {
new_conn();
}, 2000);
};
socket.onmessage = function(e) {
var msg = JSON.parse(e.data);
alert(msg);
};
};
new_conn();
控制台输出是这样的:
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 与聊天服务器断开连接
有什么问题?
谢谢
如果你使用 forever/pm2/smth 否则我认为服务器端有问题
users[data.username]["url"]=data.url;
break; // missed!
case "new_message":
问题是服务器文件上的简单对象语法错误。
我正在尝试使用 SockJS 为我的 wordpress 网站制作聊天应用程序。我的问题是客户端在连接到服务器后断开连接。我不知道。 这是我的服务器和客户端代码。
服务器:
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');
我的客户代码:
var recInterval = null;
var socket = null;
console.log("loaded js");
function new_user(){
// Get the content from the textbox
// The object to send
var send = {
action: "new_user",
username: user.username,
avatar: user.avatar,
url: user.url
};
// Send it now
socket.send(JSON.stringify(send));
}
function send_message(message){
// Get the content from the textbox
// The object to send
var send = {
action: "new_message",
username: user.username,
message: message
};
// Send it now
socket.send(JSON.stringify(send));
}
function new_conn(){
socket = new SockJS('https://example.com:9999/echo');
clearInterval(recInterval);
socket.onopen = function() {
console.log('connected to chat server');
connected=true;
new_user();
};
socket.onclose = function() {
console.log('disconnected from chat server');
socket = null;
recInterval = setInterval(function() {
new_conn();
}, 2000);
};
socket.onmessage = function(e) {
var msg = JSON.parse(e.data);
alert(msg);
};
};
new_conn();
控制台输出是这样的:
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 与聊天服务器断开连接
有什么问题? 谢谢
如果你使用 forever/pm2/smth 否则我认为服务器端有问题
users[data.username]["url"]=data.url;
break; // missed!
case "new_message":
问题是服务器文件上的简单对象语法错误。