File tree Expand file tree Collapse file tree 6 files changed +64
-5
lines changed Expand file tree Collapse file tree 6 files changed +64
-5
lines changed Original file line number Diff line number Diff line change
1
+ import { Socket } from 'socket.io'
2
+ import ChatMessagePayload from '../../common/requests/ChatMessagePayload'
3
+
1
4
class ChatController {
2
5
/**
3
6
* Broadcasts a message to all connected clients
@@ -6,7 +9,7 @@ class ChatController {
6
9
* @param data The message data
7
10
* @param socket The client socket
8
11
*/
9
- public static async broadcastMessage ( data : ChatMessagePayload , socket : SocketIO . Socket ) {
12
+ public static async broadcastMessage ( data : ChatMessagePayload , socket : Socket ) {
10
13
// TODO: Broadcast the message to all clients
11
14
/**
12
15
* VALIDATION
Original file line number Diff line number Diff line change @@ -22,13 +22,11 @@ WSS.io.on("connection", (socket: Socket) => {
22
22
const userAgent = socket . handshake . headers [ "user-agent" ] ;
23
23
console . log ( `Socket connected from ${ ip } using ${ userAgent } ` ) ;
24
24
25
+ WSS . updateClassement ( socket )
26
+
25
27
socket . on ( "place-pixel" , ( data ) => CanvasController . placePixel ( data , socket ) ) ;
26
28
socket . on ( "message" , ( data ) => ChatController . broadcastMessage ( data , socket ) ) ;
27
29
28
- socket . on ( 'ev' , ( data ) => {
29
- console . log ( data )
30
- } )
31
-
32
30
socket . on ( "disconnect" , ( ) => {
33
31
console . log ( "Socket disconnected" ) ;
34
32
} ) ;
Original file line number Diff line number Diff line change 1
1
import type http from "http" ;
2
2
import SocketIO from "socket.io" ;
3
+ import { PrismaClient } from '@prisma/client'
4
+
5
+ const client = new PrismaClient ( )
3
6
4
7
class WSS {
5
8
public static io : SocketIO . Server ;
@@ -11,6 +14,20 @@ class WSS {
11
14
}
12
15
} ) ;
13
16
}
17
+
18
+ static async updateClassement ( socket ?: SocketIO . Socket ) {
19
+ const classement = await client . account . findMany ( {
20
+ select : {
21
+ devinciEmail : true ,
22
+ placedPixels : true
23
+ } ,
24
+ orderBy : {
25
+ placedPixels : 'desc'
26
+ }
27
+ } ) ;
28
+ if ( ! socket ) this . io . emit ( 'classementUpdate' , classement )
29
+ else socket . emit ( 'classementUpdate' , classement )
30
+ }
14
31
}
15
32
16
33
export default WSS ;
Original file line number Diff line number Diff line change
1
+ interface classementItem {
2
+ devinciEmail : string ;
3
+ placedPixels : number ;
4
+ }
5
+
6
+ export default classementItem
Original file line number Diff line number Diff line change 1
1
// import { useState } from 'react'
2
+ import { useEffect , useState } from 'react' ;
2
3
import './App.css'
3
4
import LoginComponent from './pages/login'
5
+ import { socket } from './socket' ;
6
+ import classementItem from '../../common/interfaces/classementItem.interface'
4
7
5
8
function App ( ) {
9
+ const [ classement , setClassement ] = useState < classementItem [ ] > ( [ ] )
10
+ const [ isConnected , setIsConnected ] = useState ( socket . connected ) ;
11
+
12
+ useEffect ( ( ) => {
13
+ function onConnect ( ) {
14
+ setIsConnected ( true ) ;
15
+ }
16
+
17
+ function onDisconnect ( ) {
18
+ setIsConnected ( false ) ;
19
+ }
20
+
21
+ function onclassementUpdate ( data : classementItem [ ] ) {
22
+ setClassement ( data )
23
+ }
24
+
25
+ socket . on ( 'connect' , onConnect ) ;
26
+ socket . on ( 'disconnect' , onDisconnect ) ;
27
+ socket . on ( 'classementUpdate' , onclassementUpdate ) ;
28
+
29
+ return ( ) => {
30
+ socket . off ( 'connect' , onConnect ) ;
31
+ socket . off ( 'disconnect' , onDisconnect ) ;
32
+ socket . off ( 'classementUpdate' , onclassementUpdate ) ;
33
+ } ;
34
+ } , [ ] ) ;
6
35
7
36
// affichage (render)
8
37
return (
Original file line number Diff line number Diff line change
1
+ import { io } from 'socket.io-client' ;
2
+
3
+ // "undefined" means the URL will be computed from the `window.location` object
4
+ const URL = process . env . NODE_ENV === 'production' ? undefined : 'http://localhost:3000' ;
5
+
6
+ export const socket = io ( URL ) ;
You can’t perform that action at this time.
0 commit comments