-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
484 additions
and
1,608 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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
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
85 changes: 48 additions & 37 deletions
85
frontend/code/src/Components/Chat/Services/SocketsServices.ts
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 |
---|---|---|
@@ -1,43 +1,54 @@ | ||
import { io } from 'socket.io-client'; | ||
import { create } from 'zustand' | ||
|
||
interface SocketStore { | ||
socket: any; | ||
connected: boolean; | ||
|
||
const reconnect = () => { | ||
return io("http://localhost:3004", { | ||
transports: ['websocket'], | ||
}); | ||
setSocket: () => any; | ||
} | ||
export const useSocketStore:any = create((set:any) => ({ | ||
socket:null, | ||
connected:false, | ||
setSocket : () => { | ||
|
||
var s:any; | ||
|
||
const soc = set((state:any) => { | ||
if (state.socket === null){ | ||
s = io("http://localhost:3004", { | ||
transports: ['websocket'], | ||
'reconnection': true, | ||
'reconnectionDelay': 1000, | ||
'reconnectionDelayMax' : 1000, | ||
'reconnectionAttempts': 5 | ||
}); | ||
|
||
|
||
s.on('connect_error', async() => { | ||
await new Promise(r => setTimeout((r) => { | ||
console.log("inside"); | ||
s = reconnect(); | ||
s.on('connect' , () => {console.log("connected")} | ||
) | ||
},1000)) | ||
}); | ||
return {socket:s} | ||
} | ||
return state | ||
}) | ||
return soc ? soc : s; | ||
|
||
}})) | ||
|
||
export const useSocketStore = create<SocketStore>((set, get) => ({ | ||
socket: null, | ||
connected: false, | ||
setSocket: () => { | ||
let newSocket: any = null; | ||
|
||
set((state) => { | ||
if (state.socket === null) { | ||
newSocket = io("http://localhost:3004", { | ||
transports: ['websocket'], | ||
'reconnection': true, | ||
'reconnectionDelay': 1000, | ||
'reconnectionDelayMax': 1000, | ||
'reconnectionAttempts': 5 | ||
}); | ||
|
||
// Set socket | ||
set({ ...state, socket: newSocket }); | ||
|
||
newSocket.on('connect', () => { | ||
console.log('Connected!'); | ||
// Set connected state | ||
set({ ...state, connected: true }); | ||
}); | ||
|
||
newSocket.on('connect_error', async () => { | ||
await new Promise((resolve) => setTimeout(() => { | ||
// Set connected state | ||
set({ ...state, connected: false }); | ||
newSocket.connect(); | ||
resolve(newSocket); | ||
}, 1000)) | ||
}); | ||
|
||
return { ...state, socket: newSocket }; | ||
} | ||
|
||
return state; | ||
}); | ||
|
||
return newSocket; | ||
} | ||
})) | ||
|
Oops, something went wrong.