- Author: RedDec
- Created: 25 Sep 2014
JavaScript object: window.rdd
- Network
JavaScript: window.rdd.TcpSocket
TcpSocket.create (sock)->
sock.on 'data', (data)->
console.log 'Chunk', ab2str8(data)
sock.on 'connect', ()->
console.log 'Connected'
sock.send 'Hello world!'
sock.on 'close', ()->
console.log 'Closed'
sock.on 'error', (code, err, from)->
console.log 'ERROR:', from, code, err
sock.connect '127.0.0.1', 3000
connect
[TcpSocket] - socket established connection to remote hostdisconnect
[TcpSocket] - socket disconnected from remote hostclose
[TcpSocket] - socket closeddata
[ArrayBuffer] - data read from socketerror
[code, error, sendet] - raised error
create(done)
Create new TcpSocket
done
[function(TcpSocket)] - Done callback
TcpSocket(@socketId)
Create new instance of TcpSocket
socketId
[Integer] - Socket file descriptor
Public variables:
paused
[boolean] - is socket disabled for receiving messagesclosed
[boolean] - has been socket closedconnected
[boolean] - is socket connected to peer
pause(state, done)
Enables or disables the application from receiving messages from its peer. Automatically sets to true when connection established.
state
[boolean] - Pause statedone
[function(TcpSocket)] - Done callback
close(done)
Close TcpSocket. Emits 'disconnect' and 'close' events. Do nothing if not connected, but callback will be invoked.
done
[function(TcpSocket)] - Done callback
send(data, done)
Send data to peer. Emits 'error' if socket is not connected.
data
[ArrayBuffer | Object] - Message. Type of messages and end view depends ofIOC
done
[function(info)] - Done callback
connect(host, port, done)
Connect to remote address. Emits 'error' if connection failed. Emits 'connect' if connection established.
host
[string] - remote domain or ipport
[integer] - remote port numberdone
[function(info)]- Done callback if connected
disconnect(done)
Shutdown connection. Emits 'error' if socket is not connected. Emits 'disconnect' if connection shutdowned.
done
[function(TcpSocket)]- Done callback
info(done)
Get socket info. Emits 'error' if socket not connected.
done
[function(info)]- Done callback
Info (https://developer.chrome.com/apps/sockets_tcp#type-SocketInfo):
- socketId [integer]
- localAddress [string]
- localPort [integer]
- peerAddress [string]
- peerPort [integer]
- ...
JavaScript: window.rdd.TcpServer
CoffeeScript:
# Echo server
TcpServer.create (server)=>
server.on 'accept', (sock)=>
console.log 'Accept'
sock.on 'data', (data)=>
sock.send data
sock.on 'close', ()=>
console.log('Client closed')
sock.pause false, ()=>
console.log 'Reading...'
server.on 'listen', ()=>
console.log 'Listen'
server.on 'error', (code, err, from)->
console.log 'Err', code, err, from
server.listen '0.0.0.0', 3001
JavaScript:
// This is short version of echo server
window.rdd.TcpServer.create(function(server){
server.on('accept', function(client){
client.on('data', function(chunk){
client.send(chunk);
});
});
server.listen('0.0.0.0', 3001);
});
listen
[TcpServer] - socket becomes accept clientsaccept
[TcpSocket, TcpServer] - new client acceptedclose
[TcpServer] - socket closeddisconnect
[TcpServer] - stops listening and closes all child socketserror
[code, error, sender] - raised error
create(done)
Creates new instance of socket and wrap it to TcpServer
done
[function(info)] - Done callback
sockets(done)
Retrieves the list of currently opened sockets owned by the application. See: https://developer.chrome.com/apps/sockets_tcpServer#type-SocketInfo .
done
[function(List<SocketInfo>
)] - Done callback
TcpServer(socketId)
Create new instance of TcpServer based on socket descriptor
socketId
[integer] - Socket descriptor
pause(paused, done)
Set pause state
paused
[boolean] - Enable pause or notdone
[function(info)] - Done callback
listen(address, port, backlog, done, unpause)
Setup socket in listen mode and start accepting clients. Emits 'listen' on success.
address
[string] - Bind host name or ipport
[integer] - Bind port numberbacklog
[integer] (default = 1) - Clients queue sizedone
[function(TcpServer)] - Done callbackunpause
[boolean](default = true) - Unpause socket
disconnect(done)
Stop listening and close all child sockets. Emits 'disconnect' event
done
[function(TcpServer)] - Done callback
close(done)
Destroy socket. Emits 'close'.
done
[function(TcpServer)] - Done callback
info(done)
Get socket info. See: https://developer.chrome.com/apps/sockets_tcpServer#type-SocketInfo
done
[function(TcpServer)] - Done callback
JavaScript: window.rdd.UdpSocket
CoffeeScript:
UdpSocket.create (sock)=>
sock.on 'error', (code, err, from)->
console.log 'Error', code, from, err
sock.on 'data', (chunk, addr, port)->
console.log 'Data', ab2str8(chunk), 'from', addr, port
sock.bind '0.0.0.0', 0, ()=>
console.log 'Bound'
sock.info (info)=>
console.log 'Info', info
sock.send 'Hello world!', '127.0.0.1', info.localPort
JavaScript:
window.rdd.UdpSocket.create(function(sock) {
sock.on('error', function(code, err, from) {
console.log('Error', code, from, err);
});
sock.on('data', function(chunk, addr, port) {
console.log('Data', ab2str8(chunk), 'from', addr, port);
});
sock.bind('0.0.0.0', 0, function() {
console.log('Bound');
sock.info(function(info) {
console.log('Info', info);
sock.send('Hello world!', '127.0.0.1', info.localPort);
});
});
});
bind
[address, port] - socket bound to address:portclose
[UdpSocket] - socket closedjoin
[address, UdpSocket] - socket joined to multicast groupleave
[address, UdpSocket] - socket leaved multicast grouperror
[code, error, sender] - raised error
create(done)
Creates new instance of socket and wrap it to UdpSocket
done
[function(UdpSocket)] - Done callback
sockets(done)
Retrieves the list of currently opened sockets owned by the application. See: https://developer.chrome.com/apps/sockets_udp#method-getSockets
done
[function(List<SocketInfo>
)] - Done callback
UdpSocket(socketId)
Create new instance of UdpSocket
socketId
[integer] - Socket descriptor
update(properties, done)
Updates the socket properties. See: https://developer.chrome.com/apps/sockets_udp#method-update
properties
[SocketProperties] - The properties to update.done
[function(UdpSocket)] - Done callback
pause(state, done)
Pauses or unpauses a socket. A paused socket is not recieving data
state
[boolean] - Pause or notdone
[function(UdpSocket)] - Done callback
bind(address, port, done, unpause)
Binds the local address and port for the socket. For a client socket, it is recommended to use port 0 to let the platform pick a free port.
address
[string] - IP or address of machineport
[integer] - port numberdone
[function(UdpSocket)] - Done callbackunpause
[boolean] (default = true) - Automatic unpause socket
send(data, address, port, done)
Sends data on the given socket to the given address and port. The socket must be bound to a local port before calling this method.
data
[ArrayBuffer | String] - Message to sendaddress
[string] - IP or address of target machineport
[integer] - port numberdone
[function(UdpSocket)] - Done callback
close(done)
Closes the socket and releases the address/port the socket is bound to.
done
[function(UdpSocket)] - Done callback
info(done)
Retrieves the state of the given socket
done
[function(SocketInfo, UdpSocket)] - Done callback
join(address, done)
Joins the multicast group and starts to receive packets from that group. The socket must be bound to a local port before calling this method.
address
[string] - multicast addressdone
[function(UdpSocket)] - Done callback
leave(address, done)
Leaves the multicast group previously joined
address
[string] - multicast addressdone
[function(UdpSocket)] - Done callback
multicastTTL(ttl, done)
Sets the time-to-live of multicast packets sent to the multicast group.
ttl
[integer] - The time-to-live valuedone
[function(UdpSocket)] - Done callback
multicastLoopback(enable, done)
Sets whether multicast packets sent from the host to the multicast group will be looped back to the host.
enable
[boolean] - Indicate whether to enable loopback mode.done
[function(UdpSocket)] - Done callback
joinedGroups(done)
Gets the multicast group addresses the socket is currently joined to.
- done [function(UdpSocket)] - Done callback