服务器初始化

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

初始化 - 图1提示

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

初始化

只使用Socket

  • CommonJS
  • ES modules
  • TypeScript
  1. const { Server } = require("socket.io");
  2. const io = new Server({ /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });
  6. io.listen(3000);
  1. import { Server } from "socket.io";
  2. const io = new Server({ /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });
  6. io.listen(3000);
  1. import { Server } from "socket.io";
  2. const io = new Server({ /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });
  6. io.listen(3000);

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

  • CommonJS
  • ES modules
  • TypeScript
  1. const { Server } = require("socket.io");
  2. const io = new Server(3000, { /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });
  1. import { Server } from "socket.io";
  2. const io = new Server(3000, { /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });
  1. import { Server } from "socket.io";
  2. const io = new Server(3000, { /* options */ });
  3. io.on("connection", (socket) => {
  4. // ...
  5. });

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

使用HTTP服务器

  • CommonJS
  • ES modules
  • TypeScript
  1. const { createServer } = require("http");
  2. const { Server } = require("socket.io");
  3. const httpServer = createServer();
  4. const io = new Server(httpServer, { /* options */ });
  5. io.on("connection", (socket) => {
  6. // ...
  7. });
  8. httpServer.listen(3000);
  1. import { createServer } from "http";
  2. import { Server } from "socket.io";
  3. const httpServer = createServer();
  4. const io = new Server(httpServer, { /* options */ });
  5. io.on("connection", (socket) => {
  6. // ...
  7. });
  8. httpServer.listen(3000);
  1. import { createServer } from "http";
  2. import { Server } from "socket.io";
  3. const httpServer = createServer();
  4. const io = new Server(httpServer, { /* options */ });
  5. io.on("connection", (socket) => {
  6. // ...
  7. });
  8. httpServer.listen(3000);

使用HTTPS服务器

  • CommonJS
  • ES modules
  • TypeScript
  1. const { readFileSync } = require("fs");
  2. const { createServer } = require("https");
  3. const { Server } = require("socket.io");
  4. const httpServer = createServer({
  5. key: readFileSync("/path/to/my/key.pem"),
  6. cert: readFileSync("/path/to/my/cert.pem")
  7. });
  8. const io = new Server(httpServer, { /* options */ });
  9. io.on("connection", (socket) => {
  10. // ...
  11. });
  12. httpServer.listen(3000);
  1. import { readFileSync } from "fs";
  2. import { createServer } from "https";
  3. import { Server } from "socket.io";
  4. const httpServer = createServer({
  5. key: readFileSync("/path/to/my/key.pem"),
  6. cert: readFileSync("/path/to/my/cert.pem")
  7. });
  8. const io = new Server(httpServer, { /* options */ });
  9. io.on("connection", (socket) => {
  10. // ...
  11. });
  12. httpServer.listen(3000);
  1. import { readFileSync } from "fs";
  2. import { createServer } from "https";
  3. import { Server } from "socket.io";
  4. const httpServer = createServer({
  5. key: readFileSync("/path/to/my/key.pem"),
  6. cert: readFileSync("/path/to/my/cert.pem")
  7. });
  8. const io = new Server(httpServer, { /* options */ });
  9. io.on("connection", (socket) => {
  10. // ...
  11. });
  12. httpServer.listen(3000);

另请参阅:Node.js 文档

使用客户端证书身份验证:

服务器

  1. import { readFileSync } from "fs";
  2. import { createServer } from "https";
  3. import { Server } from "socket.io";
  4. const httpServer = createServer({
  5. key: readFileSync("/path/to/server-key.pem"),
  6. cert: readFileSync("/path/to/server-cert.pem"),
  7. requestCert: true,
  8. ca: [
  9. readFileSync("/path/to/client-cert.pem")
  10. ]
  11. });
  12. const io = new Server(httpServer, { /* options */ });
  13. io.engine.on("connection", (rawSocket) => {
  14. // if you need the certificate details (it is no longer available once the handshake is completed)
  15. rawSocket.peerCertificate = rawSocket.request.client.getPeerCertificate();
  16. });
  17. io.on("connection", (socket) => {
  18. console.log(socket.conn.peerCertificate);
  19. // ...
  20. });
  21. httpServer.listen(3000);

客户端

  1. import { readFileSync } from "fs";
  2. import { io } from "socket.io-client";
  3. const socket = io("https://example.com", {
  4. key: readFileSync("/path/to/client-key.pem"),
  5. cert: readFileSync("/path/to/client-cert.pem"),
  6. ca: [
  7. readFileSync("/path/to/server-cert.pem")
  8. ]
  9. });

使用HTTP/2服务器

  • CommonJS
  • ES modules
  • TypeScript
  1. const { readFileSync } = require("fs");
  2. const { createSecureServer } = require("http2");
  3. const { Server } = require("socket.io");
  4. const httpServer = createSecureServer({
  5. allowHTTP1: true,
  6. key: readFileSync("/path/to/my/key.pem"),
  7. cert: readFileSync("/path/to/my/cert.pem")
  8. });
  9. const io = new Server(httpServer, { /* options */ });
  10. io.on("connection", (socket) => {
  11. // ...
  12. });
  13. httpServer.listen(3000);
  1. import { readFileSync } from "fs";
  2. import { createSecureServer } from "http2";
  3. import { Server } from "socket.io";
  4. const httpServer = createSecureServer({
  5. allowHTTP1: true,
  6. key: readFileSync("/path/to/my/key.pem"),
  7. cert: readFileSync("/path/to/my/cert.pem")
  8. });
  9. const io = new Server(httpServer, { /* options */ });
  10. io.on("connection", (socket) => {
  11. // ...
  12. });
  13. httpServer.listen(3000);
  1. import { readFileSync } from "fs";
  2. import { createSecureServer } from "http2";
  3. import { Server } from "socket.io";
  4. const httpServer = createSecureServer({
  5. allowHTTP1: true,
  6. key: readFileSync("/path/to/my/key.pem"),
  7. cert: readFileSync("/path/to/my/cert.pem")
  8. });
  9. const io = new Server(httpServer, { /* options */ });
  10. io.on("connection", (socket) => {
  11. // ...
  12. });
  13. httpServer.listen(3000);

另请参阅: Node.js 文档

使用 Express

  • CommonJS
  • ES modules
  • TypeScript
  1. const express = require("express");
  2. const { createServer } = require("http");
  3. const { Server } = require("socket.io");
  4. const app = express();
  5. const httpServer = createServer(app);
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);
  1. import express from "express";
  2. import { createServer } from "http";
  3. import { Server } from "socket.io";
  4. const app = express();
  5. const httpServer = createServer(app);
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);
  1. import * as express from "express";
  2. import { createServer } from "http";
  3. import { Server } from "socket.io";
  4. const app = express();
  5. const httpServer = createServer(app);
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);

初始化 - 图2警告

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

更多信息在这里.

使用 Koa

  • CommonJS
  • ES modules
  • TypeScript
  1. const Koa = require("koa");
  2. const { createServer } = require("http");
  3. const { Server } = require("socket.io");
  4. const app = new Koa();
  5. const httpServer = createServer(app.callback());
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);
  1. import Koa from "koa";
  2. import { createServer } from "http";
  3. import { Server } from "socket.io";
  4. const app = new Koa();
  5. const httpServer = createServer(app.callback());
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);
  1. import * as Koa from "koa";
  2. import { createServer } from "http";
  3. import { Server } from "socket.io";
  4. const app = new Koa();
  5. const httpServer = createServer(app.callback());
  6. const io = new Server(httpServer, { /* options */ });
  7. io.on("connection", (socket) => {
  8. // ...
  9. });
  10. httpServer.listen(3000);

更多信息在这里.

使用 Nest

请参阅此处的文档。

初始化 - 图3警告

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

使用 Fastify

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

  • CommonJS
  • ES modules
  • TypeScript
  1. const fastify = require("fastify");
  2. const fastifyIO = require("fastify-socket.io");
  3. const server = fastify();
  4. server.register(fastifyIO);
  5. server.get("/", (req, reply) => {
  6. server.io.emit("hello");
  7. });
  8. server.ready().then(() => {
  9. // we need to wait for the server to be ready, else `server.io` is undefined
  10. server.io.on("connection", (socket) => {
  11. // ...
  12. });
  13. });
  14. server.listen(3000);
  1. import fastify from "fastify";
  2. import fastifyIO from "fastify-socket.io";
  3. const server = fastify();
  4. server.register(fastifyIO);
  5. server.get("/", (req, reply) => {
  6. server.io.emit("hello");
  7. });
  8. server.ready().then(() => {
  9. // we need to wait for the server to be ready, else `server.io` is undefined
  10. server.io.on("connection", (socket) => {
  11. // ...
  12. });
  13. });
  14. server.listen(3000);
  1. import fastify from "fastify";
  2. import fastifyIO from "fastify-socket.io";
  3. const server = fastify();
  4. server.register(fastifyIO);
  5. server.get("/", (req, reply) => {
  6. server.io.emit("hello");
  7. });
  8. server.ready().then(() => {
  9. // we need to wait for the server to be ready, else `server.io` is undefined
  10. server.io.on("connection", (socket) => {
  11. // ...
  12. });
  13. });
  14. server.listen(3000);

使用 µWebSockets.js

  1. import { App } from "uWebSockets.js";
  2. import { Server } from "socket.io";
  3. const app = new App();
  4. const io = new Server();
  5. io.attachApp(app);
  6. io.on("connection", (socket) => {
  7. // ...
  8. });
  9. app.listen(3000, (token) => {
  10. if (!token) {
  11. console.warn("port already in use");
  12. }
  13. });

参考: https://github.com/uNetworking/uWebSockets.js

配置

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