Comments (4)
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.
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.
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.
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)
- The NPM package is not a place for politics HOT 1
- The NPM package is not a place for politics HOT 2
- Get URL Parameters HOT 1
- Ssl using cloudflare
- Send bytes query
- broadcast
- How to connect to multiple WebSockets? HOT 1
- install of websocket and immediatly disapear
- How Can we get Connection URL from w3cwebsocket to webSocket.Server Connection
- Similar to emit socket.io HOT 1
- send to all connected clients HOT 2
- Getting vulnerable issue [email protected]
- Need more docs on the usage of client
- Unable to connect with auth info in url and special characters HOT 1
- connection.on('message', ...) - where this event is emitted ? HOT 1
- This pkg appears abandoned. I recommend switching to "ws", it's a very similar implementation HOT 4
- Malware dependency
- Update UTF-8 Validation Code
- Request to Upgrade es5-ext Dependency to Resolve Veracode Vulnerability HOT 5
- websocket doesn't work under bun HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from websocket-node.