服务器初始化
安装Socket.IO 服务器库后,您现在可以初始化服务器。可以在此处找到完整的选项列表。
提示
对于 TypeScript 用户,可以为事件提供类型提示。请检查这个.
初始化
只使用Socket
- CommonJS
- ES modules
- TypeScript
const { Server } = require("socket.io");
const io = new Server({ /* options */ });
io.on("connection", (socket) => {
// ...
});
io.listen(3000);
import { Server } from "socket.io";
const io = new Server({ /* options */ });
io.on("connection", (socket) => {
// ...
});
io.listen(3000);
import { Server } from "socket.io";
const io = new Server({ /* options */ });
io.on("connection", (socket) => {
// ...
});
io.listen(3000);
您还可以将端口作为第一个参数传递:
- CommonJS
- ES modules
- TypeScript
const { Server } = require("socket.io");
const io = new Server(3000, { /* options */ });
io.on("connection", (socket) => {
// ...
});
import { Server } from "socket.io";
const io = new Server(3000, { /* options */ });
io.on("connection", (socket) => {
// ...
});
import { Server } from "socket.io";
const io = new Server(3000, { /* options */ });
io.on("connection", (socket) => {
// ...
});
这隐式启动了一个 Node.jsHTTP 服务器,可以通过 io.httpServer
。
使用HTTP服务器
- CommonJS
- ES modules
- TypeScript
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer();
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
使用HTTPS服务器
- CommonJS
- ES modules
- TypeScript
const { readFileSync } = require("fs");
const { createServer } = require("https");
const { Server } = require("socket.io");
const httpServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
另请参阅:Node.js 文档
使用客户端证书身份验证:
服务器
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpServer = createServer({
key: readFileSync("/path/to/server-key.pem"),
cert: readFileSync("/path/to/server-cert.pem"),
requestCert: true,
ca: [
readFileSync("/path/to/client-cert.pem")
]
});
const io = new Server(httpServer, { /* options */ });
io.engine.on("connection", (rawSocket) => {
// if you need the certificate details (it is no longer available once the handshake is completed)
rawSocket.peerCertificate = rawSocket.request.client.getPeerCertificate();
});
io.on("connection", (socket) => {
console.log(socket.conn.peerCertificate);
// ...
});
httpServer.listen(3000);
客户端
import { readFileSync } from "fs";
import { io } from "socket.io-client";
const socket = io("https://example.com", {
key: readFileSync("/path/to/client-key.pem"),
cert: readFileSync("/path/to/client-cert.pem"),
ca: [
readFileSync("/path/to/server-cert.pem")
]
});
使用HTTP/2服务器
- CommonJS
- ES modules
- TypeScript
const { readFileSync } = require("fs");
const { createSecureServer } = require("http2");
const { Server } = require("socket.io");
const httpServer = createSecureServer({
allowHTTP1: true,
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { readFileSync } from "fs";
import { createSecureServer } from "http2";
import { Server } from "socket.io";
const httpServer = createSecureServer({
allowHTTP1: true,
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import { readFileSync } from "fs";
import { createSecureServer } from "http2";
import { Server } from "socket.io";
const httpServer = createSecureServer({
allowHTTP1: true,
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
另请参阅: Node.js 文档
使用 Express
- CommonJS
- ES modules
- TypeScript
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import * as express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
警告
在这里使用app.listen(3000)
将不起作用,因为它会创建一个新的 HTTP 服务器。
更多信息在这里.
使用 Koa
- CommonJS
- ES modules
- TypeScript
const Koa = require("koa");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = new Koa();
const httpServer = createServer(app.callback());
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import Koa from "koa";
import { createServer } from "http";
import { Server } from "socket.io";
const app = new Koa();
const httpServer = createServer(app.callback());
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
import * as Koa from "koa";
import { createServer } from "http";
import { Server } from "socket.io";
const app = new Koa();
const httpServer = createServer(app.callback());
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
更多信息在这里.
使用 Nest
请参阅此处的文档。
警告
NestJS v7 及以下版本依赖于 Socket.IO v2,而 NestJS v8 依赖于 Socket.IO v4。请使用兼容的客户端。
使用 Fastify
您需要注册fastify-socket.io
插件:
- CommonJS
- ES modules
- TypeScript
const fastify = require("fastify");
const fastifyIO = require("fastify-socket.io");
const server = fastify();
server.register(fastifyIO);
server.get("/", (req, reply) => {
server.io.emit("hello");
});
server.ready().then(() => {
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection", (socket) => {
// ...
});
});
server.listen(3000);
import fastify from "fastify";
import fastifyIO from "fastify-socket.io";
const server = fastify();
server.register(fastifyIO);
server.get("/", (req, reply) => {
server.io.emit("hello");
});
server.ready().then(() => {
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection", (socket) => {
// ...
});
});
server.listen(3000);
import fastify from "fastify";
import fastifyIO from "fastify-socket.io";
const server = fastify();
server.register(fastifyIO);
server.get("/", (req, reply) => {
server.io.emit("hello");
});
server.ready().then(() => {
// we need to wait for the server to be ready, else `server.io` is undefined
server.io.on("connection", (socket) => {
// ...
});
});
server.listen(3000);
使用 µWebSockets.js
import { App } from "uWebSockets.js";
import { Server } from "socket.io";
const app = new App();
const io = new Server();
io.attachApp(app);
io.on("connection", (socket) => {
// ...
});
app.listen(3000, (token) => {
if (!token) {
console.warn("port already in use");
}
});
参考: https://github.com/uNetworking/uWebSockets.js
配置
可在此处找到可用配置的完整列表。