A TCP Socket Tool, it make sending a message by tcp just like emitting an event
npm install socket-emitterA TCP Socket Tool, it make sending a message by tcp just like emitting an event.
It can send most common type of data without any extra work.
It's able to send large data.
Data will be received after 50ms.
``js
var Server = require('socket-emitter').Server;
var PORT = 3766;
var options = {
// compress: 'zlib',
}
var server = new Server(options);
server.on('connection', function(socket) {
console.log('[Server]New Connection');
socket.on('message', function(message) {
console.log(message);
// message will be 2
});
socket.emit('sendString', 'hello world');
socket.emit('sendObject', {
message: '123',
count: 23
});
var buffer = new Buffer(4);
buffer.writeUInt32BE(0x1234, 0);
socket.emit('sendBuffer', buffer);
socket.emit('endSend');
});
server.listen(PORT, function() {
console.log('Server Started');
});
`
`js
var Client = require('socket-emitter').Client;
var PORT = 3766;
var options = {
// compress: 'zlib',
}
var client = new Client(options);
client.connect(PORT, 'localhost', function() {
console.log('Client Connected');
client.emit('message', 2);
});
client.on('sendString', function(message) {
console.log(message);
// message will be ‘hello world’
});
client.on('sendObject', function(message) {
console.log('message');
// message will be {message: '123', count: 23}
});
client.on('sendBuffer', function(message) {
console.log(message);
// message will be
});
client.on('endSend', function(message) {
console.log(message);
// message will be {}
client.close(function() {
console.log('Client Closed');
});
});
`
-----------------------
is able to send most of the data types, such as String, Number, Buffer, Object, Array, null, and others.
If the data's type is Object, each of it's property value can be a SendableDataType data, such as :
`js
{
str: 'abcdef',
num: 23,
buffer: new Buffer(10),
array: [1, 'abc', / and others/],
// and others
}
`
If the data's type is Array, each entry of the Array can be a SendableDataType data, such as :
`js
[
'abc',
{obj: true},
new Buffer(20),
[1, 2, 3],
// and others
]
`$3
Create a Server instance with options.#### options
Type:
Object##### [options.compress = null]
If options.compress is equal to 'zlib',
Socket will compress data before send.Sometimes it will be slower than sending data without compress.
$3
#### connection
Server will emit a connection event when there is a new connection.The only argument is a
Socket, you can communicate with Client through it.#### error
Server will emit an error event when there is an error.#### others
Server will emit other event in different situations.$3
It's used to listen Client's connection, is same as net.Server.listen.$3
Send data to all clients.#### event
Type:
String#### arg
Type:
SendableDataType#### except
Type:
Socket | Array of SocketServer will not send data to these Sockets.
$3
It an array of online Clients.#### Server.sockets.on(event ,listener)
A method to receive data.
#### event
Type:
String#### listener
Type:
Function(socket, arg)Function will be called when any
Socket has received a data and event is same as sender's event argument.
The first argument socket is the socket witch had received this data.-----------------------
$3
Create a Client instance with options, options is same as above.Client is a subclass of Socket, the only extra method is connect.$3
It's used to connect to Server, is same as net.Socket.connect.-----------------------
$3
Method to send data.#### event
Type:
String#### arg
Type:
SendableDataType$3
Method to receive data.#### event
Type:
String#### listener
Type:
Function(arg)Function will be called when Socket has received a data and
event is same as sender's event` argument.-----------------------
More info to see at socket-emitter