Skip to content


 Zabbix
 Grafana
 Prometheus
 Threejs and TypeScript
 SocketIO and TypeScript
 Blender Topological Earth
 Sweet Home 3D
 Design Patterns Python
 Design Patterns TypeScript
   
 Course Coupon Codes
Three.js and TypeScript
Kindle Edition
$9.99 $14.99 Paperback 
$29.99 $34.99




Design Patterns in TypeScript
Kindle Edition
$9.99 $14.99 Paperback
$19.99 $24.99




Design Patterns in Python
Kindle Edition
$9.99 $14.99 Paperback
$19.99 $24.99




Server Emit to Specific Socket

Video Lecture

Server Emit to Specific Socket Server Emit to Specific Socket Server Emit to Specific Socket

Description

Emit a message to a specific socket id

img/server-emit-to-specific.jpg

./src/server/luckyNumbersGame.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
export default class LuckyNumbersGame {
    public LuckyNumbers: { [id: string]: number } = {}

    constructor() {}

    public GetWinners(number: number): string[] {
        let ret: string[] = []
        for (let key in this.LuckyNumbers) {
            if (number === this.LuckyNumbers[key]) {
                ret.push(key)
            }
        }
        return ret
    }
}

./src/server/server.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import express from 'express'
import path from 'path'
import http from 'http'
import socketIO from 'socket.io'
import LuckyNumbersGame from './luckyNumbersGame'

const port: number = 3000

class App {
    private server: http.Server
    private port: number

    private io: socketIO.Server
    private game: LuckyNumbersGame

    constructor(port: number) {
        this.port = port

        const app = express()
        app.use(express.static(path.join(__dirname, '../client')))

        this.server = new http.Server(app)
        this.io = new socketIO.Server(this.server)

        this.game = new LuckyNumbersGame()

        this.io.on('connection', (socket: socketIO.Socket) => {
            console.log('a user connected : ' + socket.id)

            this.game.LuckyNumbers[socket.id] = Math.floor(Math.random() * 10)

            socket.emit(
                'message',
                'Hello ' +
                    socket.id +
                    ', your lucky number is ' +
                    this.game.LuckyNumbers[socket.id]
            )

            socket.broadcast.emit(
                'message',
                'Everybody, say hello to ' + socket.id
            )

            socket.on('disconnect', function () {
                console.log('socket disconnected : ' + socket.id)
            })
        })

        setInterval(() => {
            let randomNumber: number = Math.floor(Math.random() * 10)
            let winners: string[] = this.game.GetWinners(randomNumber)
            if (winners.length) {
                winners.forEach((w) => {
                    this.io.to(w).emit('message', '*** You are the winner ***')
                })
            }
            this.io.emit('random', randomNumber)
        }, 1000)
    }

    public Start() {
        this.server.listen(this.port)
        console.log(`Server listening on port ${this.port}.`)
    }
}

new App(port).Start()

./src/client/client.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Client {
    private socket: SocketIOClient.Socket

    constructor() {
        this.socket = io()

        this.socket.on('message', function (message: any) {
            console.log(message)
            document.body.innerHTML += message + '<br/>'
        })

        this.socket.on('random', function (message: any) {
            console.log(message)
            document.body.innerHTML += 'Winning number is ' + message + '<br/>'
        })
    }
}

const client = new Client()