WIP on separation socket-client from chat.ts
This commit is contained in:
parent
9889600708
commit
d89c21dd2c
1 changed files with 48 additions and 0 deletions
48
frontend/src/pages/chat/socket_client.ts
Normal file
48
frontend/src/pages/chat/socket_client.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
////WORKING ON THE SEPARATE NONO FUCTIONNELING SOCKET CLIENT TO BE IMPORTED INTO CHAT.TS////
|
||||
|
||||
import io from "socket.io-client";
|
||||
|
||||
// const socket = io("wss://localhost:8888");
|
||||
|
||||
export const socket = io("wss://localhost:8888", {
|
||||
path: "/api/chat/socket.io/",
|
||||
secure: false,
|
||||
transports: ["websocket"],
|
||||
});
|
||||
|
||||
// Listen for the 'connect' event
|
||||
socket.on("connect", async () => {
|
||||
console.log("I AM Connected to the server: ", socket.id);
|
||||
// Emit a custom event 'coucou' with some data
|
||||
socket.emit("MsgObjectClient", { message: "Hello Nigel from coucou!" });
|
||||
console.log('sent console.log coucou');
|
||||
// Send a message to the server
|
||||
socket.send("Hello from the client: " + `${socket.id}`);
|
||||
console.log("MESSAGE REPLY RETURNED FROM SERVER =====>: ", socket.emit("Hello", {hello: "Hello Nigel from coucou!"}).id);
|
||||
});
|
||||
|
||||
|
||||
// Listen for messages from the server
|
||||
socket.on("MsgObjectServer", (data) => {
|
||||
console.log("Message from server:", data);
|
||||
});
|
||||
|
||||
socket.on("Hello", (data) => {
|
||||
console.log("MESSAGE REPLY RETURNED FROM SERVER =====>:", data);
|
||||
});
|
||||
|
||||
|
||||
// Disconnected
|
||||
socket.on("disconnect", (reason) => {
|
||||
console.log("Socket disconnected:", reason);
|
||||
});
|
||||
|
||||
// Socket.IO connection errors
|
||||
socket.on("connect_error", (err) => {
|
||||
console.error("Connection error:", err.message);
|
||||
});
|
||||
|
||||
// Server-side errors
|
||||
socket.on("error", (err) => {
|
||||
console.error("Socket error:", err);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue