1.1.1 • Published 6 years ago

easy-socket-browser v1.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Installing

npm install easy-socket-browser --save

Use

import EasySocket from 'easy-socket-browser';

new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  })
  .openUse((context, next) => {
    console.log("open");
    next();
  })
  .closeUse((context, next) => {
    console.log("close");
    next();
  }).errorUse((context, next) => {
    console.log("error", context.event);
    next();
  }).messageUse((context, next) => {
    if (context.res.type === 'event') {
      context.client.emit(context.res.event, context.res.args, true);
    }
    next();
  }).reconnectUse((context, next) => {
    console.log('reconnect...');
    next();
  }).remoteEmitUse((context, next) => {
    let client = context.client;
    let event = context.event;
    if (client.socket.readyState !== 1) {
      console.log('connection is not open')
    } else {
      client.socket.send(JSON.stringify({
        type: 'event',
        event: event.event,
        args: event.args
      }));
      next();
    }
  });

  let client=EasySocket.clients.get("im");
  client.connect('your server url');
  client.emit("message",'this is a message');//will send message to server
  client.on("serverMessage",data=>{
      //receive data from server
      console.log(data);
  });

config:

propertydescriptionrequiredefault
nameinstance's nametrue-
autoReconnectheartCheck and autoReconnectfalsefalse
urlurlfalse-
reconnectTimeoutreconnectTimeoutfalse10000
pingTimeoutpingTimeoutfalse15000
pongTimeoutpongTimeoutfalse3000
pingMsgpingMsgfalse'ping'

close

let client=EasySocket.clients.get(im);
client&&client.close();

open Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .openUse((context, next) => {
    console.log("open");
    next();
  })

context properties:

propertydescription
clientinstance of EasySocket
eventWebSocket onopen event result

close Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .closeUse((context, next) => {
    EasySocket.clients.delete(this.name);
    context.client.socket = null;
    context.client.connected = false;
    console.log("close");
    next();
  })

context properties:

propertydescription
clientinstance of EasySocket
eventWebSocket onclose event result

error Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .errorUse((context, next) => {
    console.log("error");
    next();
  })

context properties:

propertydescription
clientinstance of EasySocket
eventWebSocket onclose event result

message Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .messageUse((context, next) => {
    if (context.res.type === 'event') {
      context.client.emit(context.res.event, context.res.args, true);
    }
    next();
  })

context properties:

propertydescription
clientinstance of EasySocket
eventWebSocket onmessage event result
resevent.data

remoteEmit Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .remoteEmitUse((context, next) => {
    let client = context.client;
    let event = context.event;
    if (client.socket.readyState !== 1) {
      console.log('connection is not open')
    } else {
      client.socket.send(JSON.stringify({
        type: 'event',
        event: event.event,
        args: event.args
      }));
      next();
    }
  })

context properties:

propertydescription
clientinstance of EasySocket
eventevent and args

reconnect Middleware

const easySocket = new EasySocket({
    name: 'im',
    autoReconnect: true,
    pingMsg: '{"type":"event","event":"ping","args":"ping"}'
  });
easySocket
    .reconnectUse((context, next) => {
      console.log('reconnect...');
      next();
    }
  })

context properties:

propertydescription
clientinstance of EasySocket

example and online demo

chat example

online chat demo

use heartCheck code from websocket-heartbeat-js

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago