0.0.3 • Published 4 years ago

@rolancia/toilet v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

toilet

toilet 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 @rolancia/toilet


Example ( Chat )

you can see the all of features while this example.

TS

import { Server, Client, Room } from '@rolancia/toilet';

const server = new Server({port: 8888});
const chatRoom = new Room('chat-room');

server.onServerStart(() => {
    console.log('running');
});

server.onConnected((client: Client) => {
    chatRoom.enter(client);
});

server.onReceive((client: Client, jsonMessage: object) => {
    chatRoom.sendAll(jsonMessage, client);
});

server.onError((client: Client, error: Error) => {
    console.error(error);
});

server.onClose((client: Client, reason?: string) => {
    chatRoom.kick(client);
    chatRoom.sendAll({closeMsg: client.id + ' has been outed'});
});

server.start();

JS

var toilet = require('@rolancia/toilet');
var server = new toilet.Server({ port: 8888 });
var chatRoom = new toilet.Room('chat-room');

server.onServerStart(function () {
    console.log('running');
});
server.onConnected(function (client) {
    chatRoom.enter(client);
});
server.onReceive(function (client, jsonMessage) {
    chatRoom.sendAll(jsonMessage, client);
});
server.onError(function (client, error) {
    console.error(error);
});
server.onClose(function (client, reason) {
    chatRoom.kick(client);
    chatRoom.sendAll({ closeMsg: client.id + ' has been outed' });
});

server.start();

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.

onServerStart (callback)

  • callback \<Function>

called when server started.

onConnected (callback)

  • callback \<Function> * client \<Client>

called when a client is connected.

onReceive (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’.

onClose (callback)

  • callback \<Function> client \<Client> reason \<string> | \<undefined>

called when the client is disconnected.

onError (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 )

onClose (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!