Giter Site home page Giter Site logo

io-request's Introduction

IORequest

介绍

IORequest 是一个基于 socket.io 的双向 request 库。

IORequest 同时支持客户端向服务端发送请求以及服务端向客户端发送请求。

安装

node

npm install io-request

浏览器

<script src="dist/browser/io-request-client.js"></script>

用法

服务端

const server = require('http').createServer()
const io = require('socket.io')(server)

const {IORequestServer} = require('io-request')

const ioReqServer = new IORequestServer(io)

// 定义服务端 echo 请求的 handler
ioReqServer.handle('echo', ({response, data}) => {
  response(data)
})

io.on('connection', socket => {

  socket.on('io-connect', () => {
    // 向客户端端发送 echo 请求
    socket.ioRequest({method: 'echo', data: 'hello client'})
      .then(data => console.log('客户端返回:' + JSON.stringify(data)))
  })

})

server.listen(8080)

客户端

const socket = io()
const ioReqClient = new IORequestClient(socket)

// 定义客户端 echo 请求的 handler
ioReqClient.handle('echo', ({response, data}) => {
  response(data)
})

socket.on('io-connect', () => {
  socket.ioRequest({method: 'echo', data: 'hello server'})
    .then(data => console.log('服务端返回:' + data))
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.