Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected #3702

Closed
xxAROX opened this issue Jan 24, 2022 · 11 comments
Closed

Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected #3702

xxAROX opened this issue Jan 24, 2022 · 11 comments

Comments

@xxAROX
Copy link

xxAROX commented Jan 24, 2022

Details

My client will not reconnect

Node.js version

v16.13.0

Example code

const web_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);
});

const connectToSocket = (address, port) => {
	web_socket.last_address = address;
	web_socket.last_port = port;
	if (!web_socket.connected) {
		web_socket.connect(port ?? 16, address ?? "localhost");
	}
}
const reconnectToSocket = () => {
	console.log("RECONNECTING");
	console.log(web_socket.connected); //is false rn
	if (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);

Operating system

win 10

Scope

code

Module and version

dgram

Result

Connected to socket
Closed connection
RECONNECTING
false
Error [ERR_SOCKET_DGRAM_IS_CONNECTED]: Already connected
@xxAROX
Copy link
Author

xxAROX commented Jan 24, 2022

What i trying to do is a reconnect loop

@xxAROX
Copy link
Author

xxAROX commented 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 running

@Trott
Copy link
Member

Trott commented Feb 4, 2022

@xxAROX I'm not sure exactly what you're trying to do, but if it helps, this code works:

const dgram = require('dgram');

const web_socket = dgram.createSocket("udp4");

web_socket.on("connect", async (err) => {
	console.log("Connected to socket");
	web_socket.disconnect();
	setTimeout(() => reconnectToSocket(), 1000 * 10);
});

const connectToSocket = (address, port) => {
	web_socket.last_address = address;
	web_socket.last_port = port;
	if (!web_socket.connected) {
		web_socket.connect(port ?? 16, address ?? "localhost");
	}
}
const reconnectToSocket = () => {
	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:

client.send(message, 41234, 'localhost', (err) => {
  client.close();
});

The documentation could probably be improved. @marsonya is looking into doing that.

@xxAROX
Copy link
Author

xxAROX commented Feb 4, 2022

ahh okay thank you, that works

@xxAROX xxAROX closed this as completed Feb 4, 2022
@xxAROX
Copy link
Author

xxAROX commented Feb 7, 2022

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)

@xxAROX xxAROX reopened this Feb 7, 2022
@xxAROX
Copy link
Author

xxAROX commented Feb 7, 2022

ahh, i got this

@xxAROX
Copy link
Author

xxAROX commented Feb 7, 2022

but first message was received from server

@xxAROX
Copy link
Author

xxAROX commented Feb 9, 2022

@Trott

@Trott
Copy link
Member

Trott commented Feb 9, 2022

@Trott

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

This comment was marked as resolved.

@xxAROX
Copy link
Author

xxAROX commented Feb 10, 2022

ahh nvm, my issue

@xxAROX xxAROX closed this as completed Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants