Skip to content

Instantly share code, notes, and snippets.

@MarceloRab
Created November 14, 2024 14:01
Show Gist options
  • Save MarceloRab/c0deff2a3db0d96ae72baa84830ae278 to your computer and use it in GitHub Desktop.
Save MarceloRab/c0deff2a3db0d96ae72baa84830ae278 to your computer and use it in GitHub Desktop.
socket ts example
import SocketIO from "socket.io";
import io from "socket.io-client";
class Socket {
//public socketClient = io("http://localhost:8000");
//public socketClient2 = io("http:<ip:8000>");
public socketClient = io("wss:/<end-point-render>");
public connectClientSocket(): void {
this.socketClient.on("connect", () => {
console.log("Connected CLIENT Ts to socket");
});
this.socketClient.on("user update", (value) => {
console.log(`🚀 socket.ts; Cliente Ts user update - ${value.toString()}`);
});
this.socketClient.on("user removed", (value) => {
console.log(`🚀 socket.ts; Cliente Ts user removed - ${value}`);
});
}
public iniciaSocket(io: SocketIO.Server): void {
io.on("connect", (socket) => {
console.log("Connected SOCKET mae");
socket.on("user update", (object) => {
console.log(`🚀 socket.ts; EMITIDO MAE }`);
socket.broadcast.emit("user update", object);
});
socket.on("user removed", (object) => {
socket.broadcast.emit("user removed", object);
});
socket.on("disconnect", () => {
console.log("Client SOCKET MAE disconnected");
});
});
/* io.on("connect_error", (err) => {
console.log(`connect_error SOCKET MAE 🚀 => ${err.message}`);
}); */
}
/* public emitUserUpdate(idUser: string): void {
setTimeout(() => {
this.socketClient.emit("user update", idUser);
}, 9000);
}
public emitUserRemoved(idUser: string): void {
setTimeout(() => {
this.socketClient.emit("user removed", idUser);
}, 6000);
} */
}
export default new Socket();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment