daejeon v1.1.0
daejeon
daejeon is a nodejs websocket server library which provides an easy process. only JSON type is allowed. the features are simple - Server, Client and Room
Install
npm i --save daejeon
Example ( Chat )
you can see the all of features while this example.
TS
import { Server, Client, Room } from 'daejeon';
const server = new Server({port: 8888});
const chatRoom = new Room('chat-room');
server.onServerStarted(() => {
console.log('running');
});
server.onClientConnected((client: Client) => {
chatRoom.enter(client);
});
server.onMessageReceived((client: Client, jsonMessage: object) => {
chatRoom.sendAll(jsonMessage, client);
});
server.onClientError((client: Client, error: Error) => {
console.error(error);
});
server.onClientClosed((client: Client, reason?: string) => {
chatRoom.kick(client);
chatRoom.sendAll({closeMsg: client.id + ' has been outed'});
});
server.start();
JS
var daejeon = require('daejeon');
var server = new daejeon.Server({ port: 8888 });
var chatRoom = new daejeon.Room('chat-room');
server.onServerStarted(function () {
console.log('running');
});
server.onClientConnected(function (client) {
chatRoom.enter(client);
});
server.onMessageReceived(function (client, jsonMessage) {
chatRoom.sendAll(jsonMessage, client);
});
server.onClientError(function (client, error) {
console.error(error);
});
server.onClientClosed(function (client, reason) {
chatRoom.kick(client);
chatRoom.sendAll({ closeMsg: client.id + ' has been outed' });
});
server.start();
Example ( Server Test )
test client is provided. you can test your server in server-side code.
it will proceed test cases you want in regular sequence.
basically TestClient extends 'Client' class.
const testClient = new TestClient('ws://localhost:8888');
testClient.onMessageReceived(jsonMessage => {
console.log(jsonMessage);
});
testClient.startTest([
{ // SEQ 1
delayForBegin: 1000,
jsonMessage: {
data: 'Hi. I am client1!'
}
},
{ // SEQ 2
jsonMessage: {
data: 'I am hungry'
},
delayForEnd: 500
} // ...
], handler);
function handler(success: TestMessage[], failure: TestMessage[]) {
if (failure.length > 0) {
console.error(failure);
}
}
API
this library is based on ‘ws’. so it shares ‘WebSocket’ type. please refer to that on npm-ws about ‘WebSocket’ type.
Class: Server
constructor (options)
- options \<WebSocket.ServerOptions>
serverOptions \<WebSocket.ServerOptions>
returns server options you input at start.
clients \<Client[]>
returns connected client list.
start ()
starts server.
stop ()
closes server.
kick ()
- client \<Client> kicks a connected client.
clear ()
kicks all connected clients.
onServerStarted (callback)
- callback \<Function>
called when server started.
onClientConnected (callback)
- callback \<Function> * client \<Client>
called when a client is connected.
onMessageReceived (callback)
- callback \<Function>
client \<Client>
jsonMessage \<object>
called when the server receive a message. only JSON type data is allowed. so it will not call in case of parsing exception but calls ‘onError’.
onClientClosed (callback)
- callback \<Function> client \<Client> reason \<string> | \<undefined>
called when the client is disconnected.
onClientError (callback)
- callback \<Function> client \<Client> error \<Error>
called when error occurred in client side.
Class: Client
constructor (ws , id)
- ws \<WebSocket>
- id \<string> ‘id’ is set to UUIDv4 by default.
id \<string>
returns unique id. by default UUIDv4.
ip \<string> returns client’ ip address.
session \<object>
returns session object. the session can be used to store custom data you need.
send (jsonMessage) : \<boolean>
- jsonMessage \<object> sends message to this client. only JSON type data is allowed. if not, server will not send the message to other clients.
close ()
closes this client. ( its web socket also )
onClosed (callback)
called when this client is closing.
Class: Room
construntor (id)
- id \<string> ‘id’ should be unique room name. this id is not used in this library by default.
id \<string>
returns id of this room.
enter (client)
- client \<Client> adds the client to this room.
kick (client) : \<boolean>
- client \<Client> kicks the client.
kickAll ()
kicks all clients joined this room.
sendAll (jsonMessage , ignore) : \<boolean[]>
- jsonMessage \<object>
- ignore : \<Client[]> | \<undefined>
sends message to all joined this room excepting the ignored clients. only JSON type data is allowed.
Welcome your feedback
Thank you for reading!