2.1.25 • Published 5 years ago
socket.io.lite v2.1.25
socket.io.lite
a lightweight socket.io library for websocket
支持微信小程序
contain heartbeat auto reconnect
Tip
2.0 upgraded the api, using
new SocketLite ()
to create connections, no longer usingio()
to create connections.
Installation
$ yarn add socket.io.lite
# or npm
$ npm install socket.io.lite
Usage
server
const http = require('http')
const express = require('express')
const socketLite = require('../lib/server') // require('socket.io.lite')
const app = express()
...
const server = http.createServer(app)
const io = socketLite(server)
// events: open, close, error, ...
io.$on("open", client => {
console.log('open')
client.$on('testA', data => {
console.log(data)
})
client.$emit('testB', { foo: 'bar' })
})
server.listen(3002)
browser
// <script src="node_modules/socket.io.lite/dist/browser.js"></script> // window.SocketLite
// or
// const SocketLite = require('socket.io.lite/lib/browser.js')
// const SocketLite = require('socket.io.lite/dist/browser.js')
var socket = new SocketLite('ws://127.0.0.1:3002')
socket.$on('open', function () {
console.log('open')
})
socket.$emit('testA', { bar: 'foo' })
socket.$on('testB', function (data) {
console.log()
})
weapp (微信小程序)
const SocketLite = require('socket.io.lite/lib/weapp.js')
var socket = new SocketLite('ws://127.0.0.1:3002')
socket.$on('open', function () {
console.log('open')
})
socket.$emit('testA', { bar: 'foo' })
socket.$on('testB', function (data) {
console.log()
})
API
client
socket.$on(eventName, callback)
socket.$emit(eventName, data)
server
io.$on(eventName, data)
io.$emit(eventName, callback) // braodcast
socket.$on(eventName, data)
socket.$emit(eventName, callback)
about the $on !!!
BecauseJSON.stringify
andJSON.parse
are used in the current code, data is required to be an object and not to pass in a JSON format string.
Todos
- 支持直接传递基本数据类型
- 支持传递二进制数据
License
2.1.25
5 years ago
2.1.24
5 years ago
2.1.23
5 years ago
2.1.22
5 years ago
2.1.21
5 years ago
2.1.20
5 years ago
2.1.7
5 years ago
2.1.6
5 years ago
2.1.5
5 years ago
2.1.4
5 years ago
2.1.3
5 years ago
2.1.0
5 years ago
2.0.11
5 years ago
2.0.9
5 years ago
2.0.8
5 years ago
2.0.7
5 years ago
2.0.6
5 years ago
2.0.5
5 years ago
2.0.4
5 years ago
2.0.3
5 years ago
2.0.2
5 years ago
2.0.1
5 years ago
2.0.0
5 years ago
1.3.1
5 years ago
1.3.0
5 years ago
1.2.3
5 years ago
1.2.2
5 years ago
1.2.1
5 years ago
1.1.1
5 years ago
1.1.0
5 years ago
0.1.0
5 years ago