-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): add example with TypeScript
There are two issues with the typings: - on the client-side, the Emitter class is not properly imported (hence the @ts-ignore) - on the server-side, the Socket class is not exported (in order to cast it in the "connect" event)
- Loading branch information
1 parent
20ea6bd
commit a81b9f3
Showing
4 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Manager } from "socket.io-client"; | ||
|
||
const manager = new Manager("ws://localhost:8080", {}); | ||
const socket = manager.socket("/"); | ||
|
||
// @ts-ignore | ||
socket.on("connect", () => { | ||
console.log(`connect ${socket.id}`); | ||
}); | ||
|
||
// @ts-ignore | ||
socket.on("disconnect", () => { | ||
console.log(`disconnect`); | ||
}); | ||
|
||
setInterval(() => { | ||
const start = Date.now(); | ||
socket.emit("ping", () => { | ||
console.log(`pong (latency: ${Date.now() - start} ms)`); | ||
}); | ||
}, 1000); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "typescript", | ||
"version": "1.0.0", | ||
"description": "An example with TypeScript", | ||
"private": true, | ||
"scripts": { | ||
"start:server": "ts-node server.ts", | ||
"start:client": "ts-node client.ts" | ||
}, | ||
"author": "Damien Arrachequesne", | ||
"license": "MIT", | ||
"dependencies": { | ||
"socket.io": "beta", | ||
"socket.io-client": "beta", | ||
"ts-node": "^9.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Server } from "socket.io"; | ||
|
||
const io = new Server(8080); | ||
|
||
io.on("connect", (socket) => { | ||
console.log(`connect ${socket.id}`); | ||
|
||
socket.on("ping", (cb) => { | ||
console.log("ping"); | ||
cb(); | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
console.log(`disconnect ${socket.id}`); | ||
}); | ||
}); |