Code Monkey home page Code Monkey logo

Comments (4)

theturtle32 avatar theturtle32 commented on July 21, 2024

I'll need more information to be able to look into this. Is there any other output on the console? Can you paste the full stack trace?

Can you create a bare bones project that exhibits the problem so I can see it on my machine and run it through the debugger?

Sent from my iPhone

On Feb 11, 2012, at 2:35 AM, Tariqul [email protected] wrote:

Hi,

I am using websocket for my server. I am using http.createServer instance for static file server also. My problem is say a client hits my server and after that he goes into idle. After a while my server shuts down and shows the following message:

throw e; // process.nextTick error, or 'error' event on first tick

Please help me to resolve the issue.


Reply to this email directly or view it on GitHub:
#33

from websocket-node.

extley avatar extley commented on July 21, 2024

I am getting the same error. Here is the full code.

nodeServer.js

var net = require('net');

var server = net.createServer(function (socket) {
  socket.write('Echo server\r\n');
  socket.pipe(socket);
});

console.log('Server running at 127.0.0.11337');
server.listen(1337, '127.0.0.1');


Socket.js

"use strict";

process.title = 'node-chat';

var wsPort = 81;
var webSocketServer = require('websocket').server;
var http = require('http');
var history = [ ]; // entire message history
var clients = [ ]; // list of currently connected clients (users)

// Helper function for escaping input strings
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}

// HTTP server
var server = http.createServer(function(request, response) { });
server.listen(wsPort, function() { console.log((new Date()) + " Server is listening on port " + wsPort); });
// WebSocket server
var wsServer = new webSocketServer({ httpServer: server });

// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin: ' + request.origin + '.'); // accept connection - you should check 'request.origin'
var connection = request.accept(null, request.origin);
var index = clients.push(connection) - 1;
var userName = "soon!";

if (history.length > 0) {
    connection.sendUTF(JSON.stringify( { type: 'history', data: history} )); // send back chat history
}

// user sent some message
connection.on('message', function(message) {
    if (message.type === 'utf8') { // accept only text ------ htmlEntities(message.utf8Data);
      console.log((new Date()) + ' Received Message from ' + userName + ': ' + message.utf8Data);
      // we want to keep history of all sent messages
      var obj = {
          time: (new Date()).getTime(),
          text: htmlEntities(message.utf8Data),
          author: userName
      };
      history.push(obj);
      // broadcast message to all connected clients
      var json = JSON.stringify({ type:'message', data: obj });
      for (var i=0; i < clients.length; i++) {
          clients[i].sendUTF(json);
      }
    }
});

// user disconnected
connection.on('close', function(connection) {
  console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
  clients.splice(index, 1); // remove user from the list of connected clients
});

});

from websocket-node.

theturtle32 avatar theturtle32 commented on July 21, 2024

Can you please include the stack trace from the error? Otherwise there's no way for me to know what might be going on.

Thanks,
Brian

Sent from my iPhone

On Mar 30, 2012, at 5:07 AM, [email protected] wrote:

I am getting the same error. Here is the full code.

nodeServer.js

var net = require('net');

var server = net.createServer(function (socket) {
 socket.write('Echo server\r\n');
 socket.pipe(socket);
});

console.log('Server running at 127.0.0.11337');
server.listen(1337, '127.0.0.1');


Socket.js

"use strict";

process.title = 'node-chat';

var wsPort = 81;
var webSocketServer = require('websocket').server;
var http = require('http');
var history = [ ]; // entire message history
var clients = [ ]; // list of currently connected clients (users)

// Helper function for escaping input strings
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}

// HTTP server
var server = http.createServer(function(request, response) { });
server.listen(wsPort, function() { console.log((new Date()) + " Server is listening on port " + wsPort); });
// WebSocket server
var wsServer = new webSocketServer({ httpServer: server });

// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin: ' + request.origin + '.'); // accept connection - you should check 'request.origin'
var connection = request.accept(null, request.origin);
var index = clients.push(connection) - 1;
var userName = "soon!";

if (history.length > 0) {
connection.sendUTF(JSON.stringify( { type: 'history', data: history} )); // send back chat history
}

// user sent some message
connection.on('message', function(message) {
if (message.type === 'utf8') { // accept only text ------ htmlEntities(message.utf8Data);
console.log((new Date()) + ' Received Message from ' + userName + ': ' + message.utf8Data);
// we want to keep history of all sent messages
var obj = {
time: (new Date()).getTime(),
text: htmlEntities(message.utf8Data),
author: userName
};
history.push(obj);
// broadcast message to all connected clients
var json = JSON.stringify({ type:'message', data: obj });
for (var i=0; i < clients.length; i++) {
clients[i].sendUTF(json);
}
}
});

// user disconnected
connection.on('close', function(connection) {
console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
clients.splice(index, 1); // remove user from the list of connected clients
});

});


Reply to this email directly or view it on GitHub:
#33 (comment)

from websocket-node.

theturtle32 avatar theturtle32 commented on July 21, 2024

The code you posted as "nodeServer.js" is not applicable to WebSockets, it's a raw TCP server which will not work with Websockets. I'm going to close this until someone can post a detailed stack trace.

from websocket-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.