-
Notifications
You must be signed in to change notification settings - Fork 23
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
1 parent
e178812
commit 4629ff1
Showing
9 changed files
with
967 additions
and
17 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Peer from "peerjs"; | ||
|
||
export const peer = new Peer(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import io, { Socket } from "socket.io-client"; | ||
import { DefaultEventsMap } from "socket.io-client/build/typed-events"; | ||
import { setAuthSuccess, socketDisconnected } from "../redux/reducers/auth"; | ||
import { | ||
getInitialChats, | ||
recieveMessage, | ||
sendMsgSuccessful, | ||
setTotalAuthUsers, | ||
updateActiveAuthUser, | ||
updateAuthUsersInfo, | ||
updateChats, | ||
updateInactiveAuthUser, | ||
updateTotalAuthUsers, | ||
} from "../redux/reducers/chat"; | ||
import { setGlobalModal } from "../redux/reducers/globalModal"; | ||
import store from "../redux/store"; | ||
|
||
export class SocketIO { | ||
private socket: Socket<DefaultEventsMap, DefaultEventsMap>; | ||
private connected: boolean = false; | ||
|
||
public constructor(serverUrl: string, accessToken: string) { | ||
this.socket = io(serverUrl, { | ||
// Set cred to false as no need to send cookies | ||
withCredentials: false, | ||
auth: (cb) => { | ||
cb({ | ||
accessToken, | ||
}); | ||
}, | ||
}); | ||
|
||
this.connect(); | ||
} | ||
|
||
private async connect() { | ||
return await new Promise((resolve) => { | ||
this.socket.on("connect", () => { | ||
this.configure(); | ||
resolve((this.connected = true)); | ||
}); | ||
}); | ||
} | ||
|
||
private configure() { | ||
// Socket Verified | ||
this.socket?.on("signInSuccess", (mainPayload: any) => { | ||
store.dispatch(setAuthSuccess(mainPayload)); | ||
store.dispatch(getInitialChats()); | ||
this.socket?.emit("getTotalUsers"); | ||
|
||
// Total Users | ||
this.socket?.on("updateTotalUsers", (payload: string) => { | ||
store.dispatch(updateTotalAuthUsers(payload)); | ||
}); | ||
|
||
// Update Users | ||
this.socket?.on("setInitialTotalUsers", (payload: string) => { | ||
store.dispatch(setTotalAuthUsers(payload)); | ||
}); | ||
|
||
// Handle User's active status | ||
this.socket?.on("online", (payload: string) => { | ||
store.dispatch(updateActiveAuthUser(payload)); | ||
}); | ||
|
||
// Handle User's inactive status | ||
this.socket?.on("offline", (payload: string) => { | ||
store.dispatch(updateInactiveAuthUser(payload)); | ||
}); | ||
|
||
// Message was successfully sent | ||
this.socket.on("messageSentSuccessfully", (payload: any) => { | ||
store.dispatch(sendMsgSuccessful(payload)); | ||
}); | ||
|
||
this.socket.on("recieveMessage", (payload: any) => { | ||
store.dispatch(recieveMessage(payload)); | ||
}); | ||
|
||
this.socket.on("onOtherAuthUsersInfoUpdate", (payload: any) => { | ||
store.dispatch(updateAuthUsersInfo(payload)); | ||
}); | ||
|
||
// // Handle others chat switches | ||
// this.socket?.on("activeChatsSwitched", (payload: any) => { | ||
// store.dispatch(updateLastViewedChatsTimestampOfOtherUser(payload)); | ||
// }); | ||
|
||
// // Handle others active chats | ||
// this.socket?.on("friendCurrentlyOn", (payload: any) => { | ||
// store.dispatch(updateOtherUsersActiveChat(payload)); | ||
// }); | ||
|
||
// Socket Disconnected | ||
this.socket?.on("multipleSession", (payload: any) => { | ||
store.dispatch( | ||
setGlobalModal({ | ||
type: "multipleSession", | ||
params: {}, | ||
}) | ||
); | ||
}); | ||
|
||
this.socket?.on("updateExistingChats", (payload: any) => { | ||
store.dispatch(updateChats(payload)); | ||
}); | ||
|
||
// Socket Disconnected | ||
this.socket?.on("disconnect", (payload: any) => { | ||
store.dispatch(socketDisconnected()); | ||
}); | ||
}); | ||
} | ||
|
||
public connectionStatus() { | ||
return this.connected; | ||
} | ||
|
||
public getActiveSocket() { | ||
return this.socket; | ||
} | ||
} | ||
|
||
// const v = new SocketIO(); |
Oops, something went wrong.