转到主要内容
Version: 4.x

服务器初始化

安装Socket.IO 服务器库后,您现在可以初始化服务器。可以在此处找到完整的选项列表。

tip

对于 TypeScript 用户,可以为事件提供类型提示。请检查这个.

初始化

只使用Socket

const { Server } = require("socket.io");

const io = new Server({ /* options */ });

io.on("connection", (socket) => {
// ...
});

io.listen(3000);

您还可以将端口作为第一个参数传递:

const { Server } = require("socket.io");

const io = new Server(3000, { /* options */ });

io.on("connection", (socket) => {
// ...
});

这隐式启动了一个 Node.jsHTTP 服务器,可以通过 io.httpServer

使用HTTP服务器

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);

使用HTTPS服务器

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);

另请参阅: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服务器

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);

另请参阅: Node.js 文档

使用 Express

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);
警告

在这里使用app.listen(3000)将不起作用,因为它会创建一个新的 HTTP 服务器。

更多信息在这里.

使用 Koa

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);

更多信息在这里.

使用 Nest

请参阅此处的文档。

警告

NestJS v7 及以下版本依赖于 Socket.IO v2,而 NestJS v8 依赖于 Socket.IO v4。请使用兼容的客户端

使用 Fastify

您需要注册fastify-socket.io插件:

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);

使用 µWebSockets.js {#with-µwebsocketsjs}

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

配置

可在此处找到可用配置的完整列表。