mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-14 01:17:55 -05:00
Syntax:
```ts
interface ClientToServerEvents {
"my-event": (a: number, b: string, c: number[]) => void;
}
interface ServerToClientEvents {
hello: (message: string) => void;
}
const io = new Server<ClientToServerEvents, ServerToClientEvents>(httpServer);
io.emit("hello", "world");
io.on("connection", (socket) => {
socket.on("my-event", (a, b, c) => {
// ...
});
socket.emit("hello", "again");
});
```
The events are not typed by default (inferred as any), so this change
is backward compatible.
Note: we could also have reused the method here ([1]) to add types to
the EventEmitter, instead of creating a StrictEventEmitter class.
Related: https://github.com/socketio/socket.io/issues/3742
[1]: https://github.com/binier/tiny-typed-emitter
201 lines
6.4 KiB
TypeScript
201 lines
6.4 KiB
TypeScript
"use strict";
|
|
import { Server, Socket } from "..";
|
|
import type { DefaultEventsMap } from "../lib/typed-events";
|
|
import { createServer } from "http";
|
|
import { expectError, expectType } from "tsd";
|
|
|
|
// This file is run by tsd, not mocha.
|
|
|
|
describe("server", () => {
|
|
describe("no event map", () => {
|
|
describe("on", () => {
|
|
it("infers correct types for listener parameters of reserved events", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectType<Socket<DefaultEventsMap, DefaultEventsMap>>(s);
|
|
s.on("disconnect", (reason) => {
|
|
expectType<string>(reason);
|
|
});
|
|
s.on("disconnecting", (reason) => {
|
|
expectType<string>(reason);
|
|
});
|
|
});
|
|
sio.on("connect", (s) => {
|
|
expectType<Socket<DefaultEventsMap, DefaultEventsMap>>(s);
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
|
|
it("infers 'any' for listener parameters of other events", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
s.on("random", (a, b, c) => {
|
|
expectType<any>(a);
|
|
expectType<any>(b);
|
|
expectType<any>(c);
|
|
done();
|
|
});
|
|
s.emit("random", 1, "2", [3]);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("emit", () => {
|
|
it("accepts any parameters", () => {
|
|
const srv = createServer();
|
|
const sio = new Server(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
s.emit("random", 1, "2", [3]);
|
|
s.emit("no parameters");
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("single event map", () => {
|
|
interface BidirectionalEvents {
|
|
random: (a: number, b: string, c: number[]) => void;
|
|
}
|
|
|
|
describe("on", () => {
|
|
it("infers correct types for listener parameters", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<BidirectionalEvents>(srv);
|
|
expectType<Server<BidirectionalEvents, BidirectionalEvents>>(sio);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectType<Socket<BidirectionalEvents, BidirectionalEvents>>(s);
|
|
s.on("random", (a, b, c) => {
|
|
expectType<number>(a);
|
|
expectType<string>(b);
|
|
expectType<number[]>(c);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it("does not accept arguments of wrong types", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<BidirectionalEvents>(srv);
|
|
expectError(sio.on("random", (a, b, c) => {}));
|
|
srv.listen(() => {
|
|
expectError(sio.on("wrong name", (s) => {}));
|
|
sio.on("connection", (s) => {
|
|
s.on("random", (a, b, c) => {});
|
|
expectError(s.on("random"));
|
|
expectError(s.on("random", (a, b, c, d) => {}));
|
|
expectError(s.on(2, 3));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("emit", () => {
|
|
it("accepts arguments of the correct types", () => {
|
|
const srv = createServer();
|
|
const sio = new Server<BidirectionalEvents>(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
s.emit("random", 1, "2", [3]);
|
|
});
|
|
});
|
|
});
|
|
|
|
it("does not accept arguments of the wrong types", () => {
|
|
const srv = createServer();
|
|
const sio = new Server<BidirectionalEvents>(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectError(s.emit("noParameter", 2));
|
|
expectError(s.emit("oneParameter"));
|
|
expectError(s.emit("random"));
|
|
expectError(s.emit("oneParameter", 2, 3));
|
|
expectError(s.emit("random", (a, b, c) => {}));
|
|
expectError(s.emit("wrong name", () => {}));
|
|
expectError(s.emit("complicated name with spaces", 2));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("listen and emit event maps", () => {
|
|
interface ClientToServerEvents {
|
|
helloFromClient: (message: string) => void;
|
|
}
|
|
|
|
interface ServerToClientEvents {
|
|
helloFromServer: (message: string, x: number) => void;
|
|
}
|
|
|
|
describe("on", () => {
|
|
it("infers correct types for listener parameters", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(srv);
|
|
expectType<Server<ClientToServerEvents, ServerToClientEvents>>(sio);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectType<Socket<ClientToServerEvents, ServerToClientEvents>>(s);
|
|
s.on("helloFromClient", (message) => {
|
|
expectType<string>(message);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it("does not accept emit events", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectError(
|
|
s.on("helloFromServer", (message, number) => {
|
|
done();
|
|
})
|
|
);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("emit", () => {
|
|
it("accepts arguments of the correct types", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
s.emit("helloFromServer", "hi", 10);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
it("does not accept arguments of wrong types", (done) => {
|
|
const srv = createServer();
|
|
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(srv);
|
|
srv.listen(() => {
|
|
sio.on("connection", (s) => {
|
|
expectError(s.emit("helloFromClient", "hi"));
|
|
expectError(s.emit("helloFromServer", "hi", 10, "10"));
|
|
expectError(s.emit("helloFromServer", "hi", "10"));
|
|
expectError(s.emit("helloFromServer", 0, 0));
|
|
expectError(s.emit("wrong name", 10));
|
|
expectError(s.emit("wrong name"));
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|