new Server(optionsopt)
- Source:
Line Server Class
Example
const Server = require('line-socket/server');
const server = new Server({
port: 8080
});
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
Options object. Properties
|
Extends
- EventEmitterExtra
Members
(static, readonly) ErrorCode :string
- Source:
Properties:
Name | Type | Description |
---|---|---|
INVALID_OPTIONS |
string | When constructing |
INVALID_ACTION |
string | This error can be seen in rejection of |
WEBSOCKET_ERROR |
string | This error is for native websocket errors. |
Type:
- string
(static, readonly) Event :string
- Source:
Properties:
Name | Type | Description |
---|---|---|
HANDSHAKE |
string |
where |
CONNECTION |
string |
where |
HEADERS |
string |
|
ERROR |
string |
|
Type:
- string
Example
server.on('connection', (connection) => {
connection.send('hello');
...
});
// or better, you can use enums
server.on(Server.Event.CONNECTION, (connection) => {
connection.send('hello');
...
});
// If you want to authorize your client
server.on('handshake', (connection, handshake) => {
if (handshake.payload && handshake.payload.authToken == '...')
handshake.resolve({welcome: 'bro'});
else
handshake.reject(new Error('Invalid auth token'));
});
Methods
broadcast(name, payloadopt)
- Source:
Broadcasts a message to all the connected (& handshaked) clients.
Example
server.broadcast('hello', {optional: 'payload'});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | Message name |
|
payload |
any |
<optional> |
Optional message payload. |
getConnectionById(id) → (nullable) {ServerConnection}
- Source:
Gets a connection by id
Example
const connection = server.getConnectionById('someId');
if (connection) {
connection.send('hello', {optional: 'payload'});
}
Parameters:
Name | Type | Description |
---|---|---|
id |
string | Unique connection id, which can be accessed at |
Returns:
- Type
- ServerConnection
getConnections() → {Object}
- Source:
Returns a object where keys are connection id and values are ServerConnection.
Returns:
- Type
- Object
getRoom(room) → (nullable) {ServerRoom}
- Source:
Gets a room by name.
Parameters:
Name | Type | Description |
---|---|---|
room |
string | Room name |
Returns:
- Type
- ServerRoom
getRoomsOf(connection) → {Array.<string>}
- Source:
Gets all the rooms of a connection.
Parameters:
Name | Type | Description |
---|---|---|
connection |
ServerConnection |
Returns:
Array of room names.
- Type
- Array.<string>
removeFromAllRooms(connection)
- Source:
Remove a connection from all the rooms.
Parameters:
Name | Type | Description |
---|---|---|
connection |
ServerConnection |
start() → {Promise}
- Source:
Starts the server.
Example
server
.start()
.then(() => {
console.log('Server started');
})
.catch((err) => {
console.log('Server could not started', err);
});
Returns:
- Type
- Promise
stop() → {Promise}
- Source:
Stops the server.
Example
server
.stop()
.then(() => {
console.log('Server stopped');
})
.catch((err) => {
console.log('Server could not stopped', err);
});
Returns:
- Type
- Promise