Hi so i just got in to Node.JS and am loving it but i am wondering if i am
learning it the right way or using it right cause i don't want to learn it the
wrong way then i'll have to re learn it so am basically making a simple server
in Flash but i want to see if am doing it right handling client connections.
Making a new CurrentPlayer after it gets login packet setting the id and
username once they connect.
Am i doing it right?
Thank you very much.
const net = require('net');
const server = net.createServer((c) => {
c.on('end', () => {
console.log('client disconnected');
});
c.on('data', function(data) {
console.log('DATA ' + c.remoteAddress + ': ' + data);
CheckPacket(data.toString(), c);
});
c.on('close', function(data) {
console.log('CLOSED: ' + c.remoteAddress +' '+ c.remotePort);
});});
server.on('connection', function (c) {
console.log('client connected > ' + c.remoteAddress);});
server.on('error', (err) => {
throw err;});
server.listen(888, () => {
console.log('Server listening on 888');});
function CheckPacket(packet, client) {
if (packet.startsWith("<")) {
client.write("<cross-domain-policy><allow-access-from domain='*'
to-ports='888'/></cross-domain-policy>\0");
} else {
var re = /\0/g;
packet = packet.toString().replace(re, "");
var json = JSON.parse(packet);
JsonPacket(json, client);
}}
function JsonPacket(json, client) {
switch (json.type) {
case 'login':
client.name = new CurrentPlayer();
client.name.id = json.id;
console.log(client.name);
break;
default:
console.log('Unknown json: ' + json);
break;
}}
function CurrentPlayer() {
this.username = "";
this.id = 0;}
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/517213da-46bf-43d3-b2a8-cef6505295bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.