0
votes

I'm trying to make chat application using SockJS for my wordpress site. My problem is client disconnects after he connects to server. I have no idea. Here is my server and client code.

server:

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

my client code:

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

And console output is like this:

chat.js?ver=4.5.3:66 connected to chat server

chat.js?ver=4.5.3:71 disconnected from chat server

chat.js?ver=4.5.3:66 connected to chat server

chat.js?ver=4.5.3:71 disconnected from chat server

What is problem? Thanks

2

2 Answers

1
votes

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":
0
votes

Problem was a simple object syntax error on server file.