var net = require('net'), fs = require('fs'), dgram = require('dgram'), eventParser = require('../lib/eventParser.js'), pubsub = require('event-pubsub'); function emit(socket, type, data){ if(!data) data=false; this.log('dispatching event to socket'.debug, ' : ', type.data, data); var event={ type:type, data:data } if(this.config.rawBuffer){ data=new Buffer(type,this.encoding); }else{ data=eventParser.format( event ); } if(this.udp4 || this.udp6){ if(!socket.address || !socket.port){ this.log('Attempting to emit to a single UDP socket without supplying socket address or port. Redispatching event as broadcast to all connected sockets'); this.broadcast(type,data); return; } this.server.write( data, socket ); return; }; socket.write(data); }; function broadcast(type,data){ this.log('broadcasting event to all known sockets listening to '.debug, this.path.variable,' : ', ((this.port)?this.port:''), type, data); if(!data) data=false; var event={ type:type, data:data }; if(this.config.rawBuffer){ data=new Buffer(type,this.encoding); }else{ data=eventParser.format( event ); } if(this.udp4 || this.udp6){ for(var i=1, count=this.sockets.length; i0){ var e=JSON.parse(data.shift()); server.log('received event of : '.debug,e.type.data,e.data); if(e.data.id) sock.id=e.data.id; server.trigger( e.type, e.data, sock ); } } ); socket.on( 'message', function(msg,rinfo) { if (!rinfo) return; server.log('Received UDP message from '.debug, rinfo.address.variable, rinfo.port); var data; if(server.config.rawSocket){ data=new Buffer(msg,this.encoding); }else{ data=msg.toString(); } socket.emit('data',data,rinfo); } ); server.trigger( 'connect', socket ); if(server.config.rawBuffer){ return; } server.trigger( 'get.events.broadcasting', socket ); server.trigger( 'get.events.listening', socket ); } function started(socket){ server.onStart(socket) } if(!port){ server.log('starting server as'.debug, 'Unix Socket'.variable); server.server.listen( server.path, started ); server.server.maxConnections=server.maxConnections; return; } if(!server.udp4 && !server.udp4){ server.log('starting server as'.debug, 'TCP'.variable); server.server.listen( server.port, server.path, started ); return; } server.log('starting server as'.debug,((server.udp4)? 'udp4':'udp6').variable); server.server.bind( server.port, server.path ); started( { address : server.path, port : server.port } ); } } )(this) ); } }; new pubsub(server); server.on( 'get.events.broadcasting', function(socket){ server.emit( socket, 'events.broadcasting', { id : server.config.id, events : server.define.broadcast } ); } ); server.on( 'get.events.listening', function(socket){ server.emit( socket, 'events.listening', { id : server.config.id, events : server.define.listen, } ); } ) server.on( 'close', function(){ for(var i=0, count=server.sockets.length; i