You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constweb_socket=dgram.createSocket("udp4");web_socket.connected=false;web_socket.on("connect",async()=>{console.log("Connected to socket");web_socket.connected=true;web_socket.close();//for test, because the first connect works});web_socket.on("close",async()=>{console.log("Closed connection");web_socket.connected=false;setTimeout(()=>reconnectToSocket(),1000*10);});constconnectToSocket=(address,port)=>{web_socket.last_address=address;web_socket.last_port=port;if(!web_socket.connected){web_socket.connect(port??16,address??"localhost");}}constreconnectToSocket=()=>{console.log("RECONNECTING");console.log(web_socket.connected);//is false rnif(web_socket.connected){web_socket.disconnect();web_socket.close();}else{try{web_socket.connect(web_socket.last_port??16,web_socket.last_address??"localhost");}catch(e){console.error(e);}}}connectToSocket("IP",PORT);
@xxAROX I'm not sure exactly what you're trying to do, but if it helps, this code works:
constdgram=require('dgram');constweb_socket=dgram.createSocket("udp4");web_socket.on("connect",async(err)=>{console.log("Connected to socket");web_socket.disconnect();setTimeout(()=>reconnectToSocket(),1000*10);});constconnectToSocket=(address,port)=>{web_socket.last_address=address;web_socket.last_port=port;if(!web_socket.connected){web_socket.connect(port??16,address??"localhost");}}constreconnectToSocket=()=>{console.log("RECONNECTING");try{web_socket.connect(web_socket.last_port??16,web_socket.last_address??"localhost");}catch(e){console.error(e);}}connectToSocket();
I think there's some confusion about what connect() and disconnect() do. They don't establish a network connection. (UDP is connectionless!) They associate your local socket with a remote address/port. They are a relatively recent addition to the API (added in Node.js 12.x) and I imagine most use cases don't need them. Instead of using connect(), a client can call send() with the address and port:
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running
at new NodeError (node:internal/errors:371:5)
at healthCheck (node:dgram:909:11)
at Socket.send (node:dgram:643:3)
Activity
xxAROX commentedon Jan 24, 2022
What i trying to do is a reconnect loop
xxAROX commentedon Jan 24, 2022
But if i try to send a packet it thow an error,
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running. So its not connected and also not runningTrott commentedon Feb 4, 2022
@xxAROX I'm not sure exactly what you're trying to do, but if it helps, this code works:
I think there's some confusion about what
connect()anddisconnect()do. They don't establish a network connection. (UDP is connectionless!) They associate your local socket with a remote address/port. They are a relatively recent addition to the API (added in Node.js 12.x) and I imagine most use cases don't need them. Instead of usingconnect(), a client can callsend()with the address and port:The documentation could probably be improved. @marsonya is looking into doing that.
xxAROX commentedon Feb 4, 2022
ahh okay thank you, that works
xxAROX commentedon Feb 7, 2022
xxAROX commentedon Feb 7, 2022
ahh, i got this
xxAROX commentedon Feb 7, 2022
but first message was received from server
xxAROX commentedon Feb 9, 2022
@Trott
Trott commentedon Feb 9, 2022
I'm sorry. I don't understand what you're asking. Perhaps you can provide some code and explain what's not working the way you expect?
xxAROX commentedon Feb 10, 2022
xxAROX commentedon Feb 10, 2022
ahh nvm, my issue