1.1.2 • Published 6 years ago

socket-mit v1.1.2

Weekly downloads
4
License
ISC
Repository
github
Last release
6 years ago

Use socket like http

npm i --save socket-mit

example

server:

index.js

const mit = require('socket-mit');
const path = require('path');

// auto load routers/xxx.js and router/xxx/index.js;
// ignore underline begins files, like: _xxx.js
mit.load(path.resolve(__dirname, 'routers'));
mit.listen(4000);

routers/hello.js

module.exports = function(data, ws) {
  console.log(data);
  ws.dispatch({ name: 'server:hello-client', age: 100 });
};

client:

const mit = require('socket-mit/client');
var ws = mit('ws://127.0.0.1:4000');
ws.onopen = function() {
  ws.dispatch(
    'hello',
    {
      name: 'client: hello-server',
      age: 200,
    },
    res => {
      console.log('catch-server-message:', res);
    },
  );
};

auto some routers

server

const mit = require('socket-mit');
const path = require('path');
mit.load(path.resolve(__dirname, 'routers'));
mit.listen(4000);
// routers/login.js
module.exports = {
  useEmail: function(data, ws, opts) {
    // ..
  },
  usePhone: function(data, ws, opts) {
    // ..
  },
  sendEmail: {
    changePassword: function(data, ws, opts) {
      // ..
    },
    bindEmail: {
      bindNow: function(data, ws, opts) {
        // ..
      },
      bindNextDay: function(data, ws, opts) {
        // ..
      },
    },
  },
};

client

var ws = mit('ws://127.0.0.1:4000');
ws.onopen = function() {
  // load server routers/login.js function
  ws.dispatch('login.useEmail');
  ws.dispatch('login.sendEmail.changePassword');
  ws.dispatch('login.sendEmail.bindEmail.bindNow');
};

if use mysql or other database

server:

const mit = require('../serve');
const Sequelize = require('sequelize');
const { resolve } = require('path');

const sequelize = new Sequelize('test', 'root', '111Asd', { dialect: 'mysql' });
sequelize.authenticate();

// send { db: sequelize } to opts
mit.use({ db: sequelize });
mit.load(resolve(__dirname, 'routers'));
mit.listen(4000);

routers/hello.js

module.exports = function(data, ws, opts) {
  console.log(opts); // { db: sequelize }
  // use opts.db do someting
};

use local log files

set logPath, log save in local file, use pino

const mit = require('../serve');
const path = require('path');

mit.use({ logPath: path.resolve(__dirname, 'logs') });
mit.autoLoad(path.resolve(__dirname, 'routers'));
mit.listen(4000);
1.1.2

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago