服务器初始化
一旦你拥有 installed Socket.IO 服务器库,你现在就可以初始化服务器了。完整的选项列表可以在 此处 中找到。
¥Once you have installed the Socket.IO server library, you can now init the server. The complete list of options can be found here.
初始化
¥Initialization
独立式
¥Standalone
- 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);
你还可以将端口作为第一个参数传递:
¥You can also pass the port as the first argument:
- 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.js HTTP 服务器,可以通过 io.httpServer
访问它。
¥This implicitly starts a Node.js HTTP server, which can be accessed through io.httpServer
.
使用 HTTP 服务器
¥With an HTTP server
- 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 服务器
¥With an HTTPS server
- CommonJS
- ES modules
- TypeScript
const { readFileSync } = require("fs");
const { createServer } = require("https");
const { Server } = require("socket.io");
const httpsServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpsServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpsServer.listen(3000);
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpsServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpsServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpsServer.listen(3000);
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpsServer = createServer({
key: readFileSync("/path/to/my/key.pem"),
cert: readFileSync("/path/to/my/cert.pem")
});
const io = new Server(httpsServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpsServer.listen(3000);
也可以看看:Node.js 文档
¥See also: Node.js documentation
使用客户端证书身份验证:
¥With client-certificate authentication:
服务器
¥Server
import { readFileSync } from "fs";
import { createServer } from "https";
import { Server } from "socket.io";
const httpsServer = 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(httpsServer, { /* 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);
// ...
});
httpsServer.listen(3000);
客户端
¥Client
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 服务器
¥With an HTTP/2 server
- 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 文档
¥See also: Node.js documentation
使用 Express
¥With 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 服务器。
¥Using app.listen(3000)
will not work here, as it creates a new HTTP server.
更多信息在 此处。
¥More information here.
使用 Koa
¥With 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);
更多信息在 此处。
¥More information here.
使用 Nest
¥With Nest
请参阅文档 此处。
¥See the documentation here.
NestJS v7 及更低版本依赖于 Socket.IO v2,而 NestJS v8 依赖于 Socket.IO v4。请使用 兼容客户端。
¥NestJS v7 and below relies on Socket.IO v2, while NestJS v8 relies on Socket.IO v4. Please use a compatible client.
使用 Fastify
¥With Fastify
你需要注册 fastify-socket.io
插件:
¥You need to register the fastify-socket.io
plugin:
- 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({ port: 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({ port: 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({ port: 3000 });
使用 µWebSockets.js
¥With µWebSockets.js
import { App } from "uWebSockets.js";
import { Server } from "socket.io";
const app = 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
¥Reference: https://github.com/uNetworking/uWebSockets.js
使用 Hono (Node.js)
¥With Hono (Node.js)
- CommonJS
- ES modules
- TypeScript
const { Hono } = require("hono");
const { serve } = require("@hono/node-server");
const { Server } = require("socket.io");
const app = new Hono();
const httpServer = serve({
fetch: app.fetch,
port: 3000,
});
const io = new Server(httpServer, {
/* options */
});
io.on("connection", (socket) => {
// ...
});
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { Server } from "socket.io";
const app = new Hono();
const httpServer = serve({
fetch: app.fetch,
port: 3000,
});
const io = new Server(httpServer, {
/* options */
});
io.on("connection", (socket) => {
// ...
});
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { Server } from "socket.io";
import type { Server as HTTPServer } from "node:http";
const app = new Hono();
const httpServer = serve({
fetch: app.fetch,
port: 3000,
});
const io = new Server(httpServer as HTTPServer, {
/* options */
});
io.on("connection", (socket) => {
// ...
});
更多信息在 此处。
¥More information here.
选项
¥Options
可用选项的完整列表可在 此处 中找到。
¥The complete list of available options can be found here.