I am trying to find the id of clients that connect to my socket.io/node.js server using the method described in the top answer here how to get session id of socket.io client in Client but when I do I get the error message
C:\Games\My games\Newserver\Server\server.js:5
playerlist[playerlist.length+1] = [client.id,username]
^
ReferenceError: client is not defined
at SocketNamespace.<anonymous> (C:\Games\My games\Newserver\Server\server.js
:5:37)
at SocketNamespace.EventEmitter.emit [as $emit] (events.js:117:20)
at connect (C:\Games\My games\Newserver\Server\node_modules\socket.io\lib\na
mespace.js:292:10)
at C:\Games\My games\Newserver\Server\node_modules\socket.io\lib\namespace.j
s:308:13
at SocketNamespace.authorize (C:\Games\My games\Newserver\Server\node_module
s\socket.io\lib\namespace.js:252:5)
at SocketNamespace.handlePacket (C:\Games\My games\Newserver\Server\node_mod
ules\socket.io\lib\namespace.js:302:14)
at Manager.handleClient (C:\Games\My games\Newserver\Server\node_modules\soc
ket.io\lib\manager.js:698:32)
at Manager.handleUpgrade (C:\Games\My games\Newserver\Server\node_modules\so
cket.io\lib\manager.js:618:8)
at Server.<anonymous> (C:\Games\My games\Newserver\Server\node_modules\socke
t.io\lib\manager.js:123:10)
at Server.EventEmitter.emit (events.js:106:17)
My code is as follows
var io = require('socket.io').listen(1337); //Tells server to use socket.io and to listen on port 1337
var playerlist= new Array(); //Array to store player usernames & client ids of these players
io.sockets.on("connection", function(socket) {
playerlist[playerlist.length+1] = [client.id,username] //Writing to array, crashes here at client.id
socket.on("username", function(data) {
var str = "[Server] User "
var str2 = data
var str3 = " connected."
var finalstr = str.concat(str2.concat(str3))
socket.broadcast.send(finalstr)
socket.send("[Server] Connected")
});
});
Does anyone know how to fix this? I can only assume I haven't require()d something that I should have but I don't know what.
You've got no variable client. Use socket instead.
Change the line
playerlist[playerlist.length+1] = [client.id,username]
to
playerlist[playerlist.length] = [socket.id,username]
By the way, I think you should use playerlist.length here.
Related
I'm trying to print a document on the second paper tray with IPP (Internet Printing Protocol). I'm using this npm IPP-Library.
But at any time i try to print a document my printer shows a message that i need to add paper to the first paper tray and the console output of says Printed: successful-ok.
var ipp = require("ipp");
var PDFDocument = require("pdfkit");
var concat = require("concat-stream");
var doc = new PDFDocument;
doc.text("Hello World");
doc.pipe(concat(function (data) {
var printer = ipp.Printer("MY_URL");
var file = {
"operation-attributes-tag": {
"requesting-user-name": "admin",
'attributes-charset': 'utf-8',
'attributes-natural-language': 'de'
},
"printer-attributes": {
"media-col": {
"media-source": "tray-2"
},
},
data: data
};
printer.execute("Print-Job", file, function (err, res) {
console.log("Printed: " + res.statusCode);
});
}));
doc.end();
The other variant i tried is following (from here):
var PDFDocument = require("pdfkit");
let fs = require('fs')
var ipp = require('ipp');
var uri = "http://10.1.205.71";
var msg = new Buffer(
'0200'+ //Version
'000201e6d5f2'+
'01'+ //Operation attributes tag (your information in the Operation attributes might be different)
'47'+ //charset tag
'0012'+ //length
'617474726962757465732d63686172736574'+ //attributes-charset
'0005'+ //length
'7574662d38'+ //utf-8
'48'+ //natural language tag
'001b'+ //length
'617474726962757465732d6e61747572616c2d6c616e6775616765'+//attributes-natural-language
'0002'+//length
'656e'+ //en
'45'+ // URI tag
'000b'+ //length
'7072696e7465722d757269'+ //printer-uri
'0012'+//length
'687474703a2f2f31302e312e3230352e3731'+//http://10.1.205.71
'49'+ //mimeMediaType tag
'000f'+ //length
'646f63756d656e742d666f726d6174'+ //document format
'000f'+ //length
'6170706c69636174696f6e2f706466'+ //application/pdf
'02'+ //job attributes tag
'34'+ //begin collection
'0009'+ //length
'6d656469612d636f6c'+ //media-col
'0000'+ //value length
'4a'+ //collection entry
'0000'+ //name length
'000c'+ //value length
'6d656469612d736f75726365'+ //media-source
'44'+ // collection entry
'0000'+ //name length
'0006'+ //value length
'747261792d32'+ //tray-2
'37'+ //end of collection
'00000000'+ //name length and value length
'03', 'hex');
var doc = new PDFDocument;
doc.text("Hello World");
var buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', function(){
var buf = Buffer.concat(buffers);
var catBuf = Buffer.concat([msg, buf]);
ipp.request(uri, catBuf, function(err, res){
if(err){
return console.log(err);
}
console.log(JSON.stringify(res,null,2));
});
});
doc.end();
But then i got this error message:
{
Error
at new IppResponseError (/Users/alex/dev/print/printing/node_modules/ipp/lib/request.js:72:17)
at ClientRequest.<anonymous> (/Users/alex/dev/print/printing/node_modules/ipp/lib/request.js:40:8)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at Socket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
name: 'IppResponseError',
statusCode: 400,
message: 'Received unexpected response status 400 from the printer',
stack: 'Error\n at new IppResponseError (/Users/alex/dev/print/printing/node_modules/ipp/lib/request.js:72:17)\n at ClientRequest.<anonymous> (/Users/alex/dev/print/printing/node_modules/ipp/lib/request.js:40:8)\n at Object.onceWrapper (events.js:293:19)\n at emitOne (events.js:96:13)\n at ClientRequest.emit (events.js:191:7)\n at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:522:21)\n at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)\n at Socket.socketOnData (_http_client.js:411:20)\n at emitOne (events.js:96:13)\n at Socket.emit (events.js:191:7)' }
400 'response'
My printer does not support IPP, but i shared it on my Macbook, which provides an IPP service for all shared printers.
If i'm using the first paper tray and have paper in there everything is fine, but for my project it is necessary to print on other trays, too.
The attributes' list returned from Get-Printer-Attributes lists among other trays the second paper as supported media-source, but only the first paper tray works.
Does anyone have an idea how to print successfully on another paper tray?
Update: I also tried another printer, but i got the same error.
Update 22.06.17: It's still confused and don't have any clue how to fix this.
It appears that this pull request might be able to solve the issue you're having. Until the author of ipp merges the pull request, you can update your npm package to point to that patch by running the following in your project directory:
npm i --save ipp#github:jaymcaliley/ipp
The response to the request that you are sending is with the status code of 400 to the request that was issued to the printer.
This can be seen here on line 30.
This can be caused by a firewall configuration or wrong network setting.
You need to specify correctly the URL for the printer like in this example and to check if this URL is valid and the printer responds to it:
var printer = ipp.Printer("http://NPI977E4E.local.:631/ipp/printer");
It's been a while since i asked for help. Thanks to all for their contribution =)
I tried all suggested solutions from here and Github, but none of them worked, but I found a solution to solve my issue.
var ipp = require("ipp");
var PDFDocument = require("pdfkit");
var concat = require("concat-stream");
var doc = new PDFDocument;
doc.text("Hello World");
doc.pipe(concat(function (data) {
var printer = ipp.Printer("MY_URL");
var file = {
"operation-attributes-tag": {
"requesting-user-name": "admin",
'attributes-charset': 'utf-8',
'attributes-natural-language': 'de'
},
"printer-attributes": {
// OLD WAY WHICH DOES NOT WORK
//"media-col": {
// "media-source": "tray-2"
//},
},
// SOLUTION
"job-attributes-tag":{
"media": ["tray-2"]
},
data: data
};
printer.execute("Print-Job", file, function (err, res) {
console.log("Printed: " + res.statusCode);
});
}));
doc.end();
I tried this, because here (4.2.11) is media described with:
The values for "media" include medium-names, medium-sizes, input-
trays and electronic forms so that one attribute specifies the media.
I'm getting this error: "Cannot read property 'insert' of undefined" when trying to insert data into a database. The error shows on:
db.coordinates.insert({ "x" : "data.x", "y" : "data.y"})
Database name - "node5"
Collection name - "coordinates"
// Including libraries
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var static = require('node-static'); // for serving files
//db connection
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/node5');
var url = 'mongodb://localhost:27017/node5';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server.");
db.close();
});
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(8080);
// If the URL of the socket server is opened in a browser
function handler(request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
}).resume();
}
// Delete this row if you want to see debug messages
io.set('log level', 1);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
// Listen for mouse move events
socket.on('post', function (data) {
console.log('posted');
console.log(data);
socket.broadcast.emit('posted', data); // Broadcasts event to everyone except originating client
db.coordinates.insert({ "x" : "data.x", "y" : "data.y"})
});
});
When writing an answer, please note that I'm new to node.js and I might not understand if you tell the answer in a complex way:)
If you are using monk for your project, then you can drop the mongodb module, since it's functionality is being wrapped up by monk. From Monk's documentation, you should be doing something like:
const monk = require('monk');
const db = monk('localhost:27017/node5')
const coordinates = db.get('coordinates');
Now that you have a reference to your coordinates collection, you can use it later in your code:
coordinates.insert({ x: data.x, y: data.y });
I hope this is easy enough to understand. If it is still confusing, then please comment below and I'll elaborate further :)
I am trying to send a message from NodeJS server to client using socket.io
However, I found the same practice all over the internet, which is wrapping the emit with io.on('connection', handler) and then making the server listen on a special "channel" event like so:
var io = require('socket.io')();
var socketioJwt = require('socketio-jwt');
var jwtSecret = require('./settings').jwtSecret;
var User = require('./models/users').User;
io.set('authorization', socketioJwt.authorize({
secret: jwtSecret,
handshake: true
}));
var sockets = [];
io.on('connection', function(socket) {
sockets.push(socket);
});
sendLiveUpdates = function(gameSession) {
console.log(sockets);
}
exports.sendLiveUpdates = sendLiveUpdates;
exports.io = io;
My problem is: I want to emit messages outside this on connection wrapper, example from my routes or other scripts. Is it possible?
Thanks.
Yes. You just need to keep a reference to the socket.
// Just an array for sockets... use whatever method you want to reference them
var sockets = [];
io.on('connection', function(socket) {
socket.on('event', function() {
io.emit('another_event', message);
});
// Add the new socket to the array, for messing with later
sockets.push(socket);
});
Then somewhere else in your code...
sockets[0].emit('someEvent');
What I usually do is assign new clients a UUID and add them to an object keyed by this UUID. This comes in handy for logging and what not as well, so I keep a consistent ID everywhere.
I'm trying to recreate the functionality of a hardware serial server with Node and it's actually working, but I'm getting errors from socket instances that have been closed.
Here's a simplified version of the app to show what I'm doing...
var net = require('net');
var SerialPort = require('serialport');
var connectionCounter = 0;
var port = new SerialPort('/dev/ttyUSB0', function () {
var server = net.createServer();
server.on('connection',function(socket) {
connectionCounter++;
var connNumber = connectionCounter;
socket.on('error', function () {
console.log('socket ' + connNumber + ' errored');
});
socket.on('data', function(data) {
port.write(data);
});
port.on('data', function(data) {
socket.write(data);
});
});
server.listen(8887, '127.0.0.1');
}
});
So the first chunk of code that's sent into the 8887 port works fine, and it returns the data back out through the socket. The errors start on the second chunk. In the example, I'm keeping a count of the socket instances and outputting the socket instance number with the error. So as the program runs, the number of sockets instances keeps going up. The most recent instance will eventually handle the data, but I can't figure out what I need to delete to clean up all of the previous socket instances so they'll stop trying to process the incoming data.
I've tried socket.end() and socket.destroy(), but those don't seem to work . Do I need to go as far as deleting the server itself and recreating it?
If anyone ever finds this and cares about what was going wrong, I was setting an event listener on the serialport object every time a new net socket was created. So even though I was deleting the socket every time it was closed, the serialport listener was trying to send data to all of the old deleted sockets. So the solution was to removeListeners from the serialport object upon closing the net socket.
you can use array for storing sockets later on you can delete. this is sample code hope you got the idea
var net = require('net');
var SerialPort = require('serialport');
var connectionCounter = 0;
var mySockets = [];
var port = new SerialPort('/dev/ttyUSB0', function () {
var server = net.createServer();
server.on('connection',function(socket) {
mySockets.push(socket);
connectionCounter++;
var connNumber = connectionCounter;
socket.on('error', function () {
console.log('socket ' + connNumber + ' errored');
});
socket.on('data', function(data) {
port.write(data);
});
port.on('data', function(data) {
socket.write(data);
});
});
server.listen(8887, '127.0.0.1');
}
//get the sockets you want to delete
var s = mySockets.pop();
s = null;
});
I followed this tutorial (http://www.hongkiat.com/blog/node-js-server-side-javascript/) and when running the next to last script (for creating a static server) the command prompt says "Server running on port 8080", but when trying to access it at localhost:8080 I just get a webpage is unavailable error.
I have made an rule in the firewall to allow access to 8080 as well.
What could be causing this? Should i be trying to access the page from another address?
When I try to access the page i get the following error message in cmd:
C:\Users\id122302\Documents\test.js:11
path.exists(full_path,function(exists)
^
TypeError: undefined is not a function
at Server.<anonymous> (C:\Users\id122302\Documents\test.js:11:7)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
This is my code:
var sys = require("sys");
my_http = require("http");
path = require("path");
url = require("url");
filesys = require("fs");
//Create Server
my_http.createServer(function(request,response)
{
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
path.exists(full_path,function(exists)
{
if (!exists)
{
response.writeHeader(404, {"Content-Type":"text/plain"});
response.write("404 Not Found\n");
response.end();
}
else
{
filesys.readFile(full_path, "binary", function(err,file)
{
if (err)
{
response.writeHeader(500,{"Content-Type":"text/plain"});
response.write(err + "\n");
response.end();
}
else
{
response.writeHeader(200);
response.write(file,"binary");
response.end();
}
});
}
});
}).listen(8080);
console.log("Server Running on 8080");
Your server shows an exception and a line number => go for that place !
As observed by #maniacnero, there's no more such thing as path.exists in the API. There's an fs.exists but it's been deprecated, to avoid abusive usage in node's concurrent context.
The feared scenario would be :
you check asynchronously if a file exists.
some other routine deletes/renames it in the meanwhile, or something else on the server does.
you think that the file exists so you try to open it and confidently don't handle the error case.
So the lessons learnt here are :
do things atomically
always deal with failures right away
Provided you stick to this discipline, there's no need for such thing as fs.exists. Here's a modified version of your code :
var sys = require("sys");
var http = require("http");
var path = require("path");
var url = require("url");
var fs = require("fs");
var port = 8080;
http.createServer(function(request,response) {
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
fs.readFile(full_path, function(err, file) {
if (err) {
response.writeHeader(404, {"Content-Type":"text/plain"});
response.write("404 Not Found\n");
response.end();
} else {
response.writeHeader(200);
response.write(file);
response.end();
}
});
}).listen(port);
console.log("Server Running on " + port);
I also removed those "binary" thingys, that are way outdated and not documented in the API either !
Playing around with sample code is a nice way to learn, but only if you don't do it blindly. ;) Especially in a weakly typed language building on a fast changing API and where myriads of tutorials have been written by utter beginners. This is your friend : https://nodejs.org/api/