hola estoy intentando hacer un web-socket para usar con p5js.org, pero la única cosa que me funciona es esto:https://socketservice.herokuapp.com/ la sintaxis es la siguiente:
SocketService.init( 'my-awesome-room', socket => {
socket.on( 'my-event', data => console.log( data ) )
htmlbutton.addEventListener("click",socket.emit( 'my-event', { msg: "hello from button" } ))
socket.on( 'room-joined', room => {
console.log( `socket joined ${ room }` )
socket.emit( 'my-event', { msg: `hello from ${ window.location }` } )
} )
} )
y me gustaría usar algo mas simple:
function Socket(room,callback){
this.room=room;
this.recived=callback;
this.send=function(obj){
}
}
juntando los dos códigos... cuando recibo un mensaje llamo al callback y ningún problema, la cosa es cuando quiero enviar un mensaje desde cualquier parte de mi código debería llamar Socket.send(object) y esto disparar un evento dentro del socketservice como el del botón y ejecutar el socket.emit:?
button.addEventListener("click",callback)
entonces como disparo un evento virtual?
o se os ocurre algo mejor?
gracias:)