服务端API
使用express+http创建server:
var app = require('express')() //初始化express,app作为HTTP服务器的回调函数
var http = require('http').Server(app)
var io = require('socket.io')(http) //传入http对象初始化socket.io的实例
IO
io.sockets
- (Namespace)
命名空间默认为/
amespace {
name: '/',
server:
Server {
nsps: { '/': [Circular] },
parentNsps: Map {},
_path: '/socket.io',
_serveClient: true,
parser:
{ protocol: 4,
types: [Array],
CONNECT: 0,
DISCONNECT: 1,
EVENT: 2,
ACK: 3,
ERROR: 4,
BINARY_EVENT: 5,
BINARY_ACK: 6,
Encoder: [Function: Encoder],
Decoder: [Function: Decoder] },
encoder: Encoder {},
_adapter: [Function: Adapter],
_origins: '*:*',
sockets: [Circular],
eio:
Server {
clients: {},
clientsCount: 0,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [Object],
_events: [Object],
_eventsCount: 1 },
httpServer:
Server {
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
_connections: 0,
_handle: null,
_usingSlaves: false,
_slaves: [],
_unref: false,
allowHalfOpen: true,
pauseOnConnect: false,
httpAllowHalfOpen: false,
timeout: 120000,
keepAliveTimeout: 5000,
_pendingResponseData: 0,
maxHeadersCount: null,
[Symbol(asyncId)]: -1 },
engine:
Server {
clients: {},
clientsCount: 0,
wsEngine: 'ws',
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 100000000,
transports: [Array],
allowUpgrades: true,
allowRequest: [Function: bound ],
cookie: 'io',
cookiePath: '/',
cookieHttpOnly: true,
perMessageDeflate: [Object],
httpCompression: [Object],
initialPacket: [Array],
ws: [Object],
_events: [Object],
_eventsCount: 1 } },
sockets: {},
connected: {},
fns: [],
ids: 0,
rooms: [],
flags: {},
adapter: Adapter { nsp: [Circular], rooms: {}, sids: {}, encoder: Encoder {} } }
常用的API如下:
io.on('connection', (socket) => {
// 发送给当前客户端
socket.emit('hello', 'can you hear me?', 1, 2, 'abc');
// 发送给所有客户端,除了发送者
socket.broadcast.emit('broadcast', 'hello friends!');
// 发送给同在 'game' 房间的所有客户端,除了发送者
socket.to('game').emit('nice game', "let's play a game");
// 发送给同在 'game1' 或 'game2' 房间的所有客户端,除了发送者
socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");
//发送给在线的所有客户端
io.emit('new message', 'an event sent to all connected clients');
// 发送给同在 'game' 房间的所有客户端,包括发送者
io.in('game').emit('big-announcement', 'the game will start soon');
// 发送给同在 'myNamespace' 命名空间下的所有客户端,包括发送者
io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');
// 发送给指定 socketid 的客户端(私密消息)
socket.to().emit('hey', 'I just met you');
// 包含回执的消息
socket.emit('question', 'do you think so?', function (answer) {});
// 不压缩,直接发送,默认压缩
socket.compress(false).emit('uncompressed', "that's rough");
// 发送给当前 node 实例下的所有客户端(在使用多个 node 实例的情况下)
io.local.emit('hi', 'my lovely babies');
};
看完API,下一篇来看一个聊天室demo