Klaus1: Frage zu Node.js und 2 laufenden Listener (net und websocket)

Beitrag lesen

Hallo,

ich stelle mal den logischen Teil des Quellcodes hier rein. Im Grunde sind das also zwei Chats und mein Versuch ist es, dass sich beide Parteien untereinander unterhalten können. D.h. schreibt die eine Partei etwas, soll die andere Partei davon erfahren und auch antworten können.

LG Klaus

// Load the TCP Library
var net = require('net');

// Load Websocket Library
var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({host:'localhost',port: 4711});


// Keep track of the socket clients
var s_clients = [];

// Start a TCP Server
net.createServer(function (socket) {
	// Identify this client
	socket.name = socket.remoteAddress + ":" + socket.remotePort 

	// Put this new client in the list
	s_clients.push(socket);

	// Send a nice welcome message and announce
	socket.write("Welcome " + socket.name + "\n");
	broadcast(socket.name + " joined the chat\n", socket);

	// Handle incoming messages from clients.
	socket.on('data', function (data) {
		test = JSON.stringify(data);
		broadcast(socket.name + " wrote > " + data, socket);
		console.log(test);
		console.log(convert_hex2text(data));
	});

	// Remove the client from the list when it leaves
	socket.on('end', function () {
		s_clients.splice(s_clients.indexOf(socket), 1);
		broadcast(socket.name + " left the chat.\n");
	});
	socket.on('close', function () {
		s_clients.splice(s_clients.indexOf(socket), 1);
		broadcast(socket.name + " left the chat.\n");
	});

	socket.on('error', function (err) {
		process.stdout.write("Got a socket error... client removed from list\n");
		s_clients.splice(s_clients.indexOf(socket), 1);
		broadcast(socket.name + " left the chat.\n");
		process.stdout.write(err.stack);
	});

	socket.on('timeout', function () {
		s_clients.splice(s_clients.indexOf(socket), 1);
		console.log('Socket timed out!');
		socket.end();
	});

	// Send a message to all clients
	function broadcast(message, sender) {
		s_clients.forEach(function (client) {
			// Don't want to send it to sender
			if (client === sender) {
				try {
					//	client.write('Got your message!');
					return;
				}
				catch (err) {
					console.log('cannot send message:'+err.stack);
				}
			}
			try {
				client.write(message);					// Sends Socket-Message to client
			}
			catch (err) {
				console.log('cannot send message: '+err.stack);
			}
		});
		// Log it to the server output too
		process.stdout.write(message);
	}


}).listen(1234);

// Put a friendly message on the terminal of the server.
console.log("Socket-Server listening on port 1234\n");


// ==========================================================================================================================================
// HERE BEGINS THE WEBSOCKET PART
// ==========================================================================================================================================

var ws_clients = [];
var chat = {

	run: function(){
		wss.on('connection', function(ws) {
			ws.on('message', function(message) {
				console.log('received: %s', message);
				chat.dispatch(ws, message);
			});
			
			ws.on('close', function() {
				chat.removeClient(ws);
			});
		});	
	},
	
	removeClient: function(ws){
		ws_clients.forEach(function (client) {
			if(client === ws){
				console.log('remove client '+client);
				ws_clients.splice(ws_clients.indexOf(ws), 1);
			}
		});
	},
	
	registerClient: function(ws, client){
		ws_clients.push(ws);
	},
	
	broadcast: function(message,fromSocket){
		chat.broadcastCommand(message,fromSocket);
	},

	broadcastCommand: function(cmd,fromSocket){
		ws_clients.forEach(function (client) {
			if (client === fromSocket) {
				// do nothing, otherwise it's a simple echo server
				return;
			} else {
				try{		
					client.socket.send(cmd);
					client.write(cmd);
				} catch(error){
					clients.splice(clients.indexOf(client.socket), 1);
					console.log(error);
				}
			}
		});
	},
	
	dispatch: function(ws, message){
	
		var cmd = '';
		var param = '';

		if(message.includes('/')){
			cmd = message.split(' ')[0];				// get command, left part of message 
			param = message.split(' ')[1];				// get parameter, right part of message
			param = param.substring(0,param.length-1);		// cut off last char 
		}
		switch(cmd){
			case '/connect':
				var msg = param.replace(' ','').replace(/(<([^>]+)>)/ig,"");
				if(msg != ''){
					chat.registerClient(ws, msg);
				}
				break;
			default:
				chat.broadcast(param, ws);
				console.log('unkown message: '+message);
		}
		
	}

}

chat.run();

// Put another friendly message on the terminal of the server.
console.log("WebSocket-Server listening on port 4711\n");