Constructor
new SocketServer()
Defines a SocketServer around a socket.io server.
Creating a server using socket.io can't be done without launching the server and start listening to messages. SocketServer helps creating a socket server and add namespaces to it without starting the server.
- Source:
Example
var openVeoApi = require('@openveo/api');
var namespace1 = new openVeoApi.socket.SocketNamespace();
var namespace2 = new openVeoApi.socket.SocketNamespace();
var server = new openVeoApi.socket.SocketServer();
// Listen to a message on first namespace
namespace1.on('namespace1.message', function() {
console.log('namespace1.message received');
});
// Listen to a message on second namespace
namespace2.on('namespace2.message', function() {
console.log('namespace2.message received');
});
// Add namespace1 to the server
server.addNamespace('/namespace1', namespace1);
// Start server
server.listen(80, function() {
console.log('Socket server started');
namespace.emit('namespace1.message');
// Adding a namespace after the server is started will also work
server.addNamespace('/namespace2', namespace2);
namespace2.emit('namespace2.message');
});
Members
io :Object
The Socket.io server.
Type:
- Object
- Source:
(readonly) namespaces :Object
The list of namespaces added to the server indexed by names.
Type:
- Object
- Source:
Methods
addNamespace(name, namespace)
Adds a namespace to the server.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The namespace name |
namespace |
module:socket/SocketNamespace~SocketNamespace | The socket namespace to add |
- Source:
close()
Closes the server.
- Source:
getNamespace(name) → {module:socket/SocketNamespace~SocketNamespace}
Gets a namespace.
Parameters:
Name | Type | Description |
---|---|---|
name |
String | The namespace name |
- Source:
Returns:
The namespace
listen(port, allowedOrigins) → {function}
Starts the Socket server and mount namespaces.
Parameters:
Name | Type | Description |
---|---|---|
port |
Number | The port to use for the server |
allowedOrigins |
Array | The list of authorized origins |
- Source:
Returns:
callback Function to call when its done
- Type
- function