Esente
Esente
Explore posts from servers
CDCloudflare Developers
Created by Esente on 4/16/2024 in #workers-help
TCP Socket does not seem to work on Worker
Still does not work.
5 replies
CDCloudflare Developers
Created by Esente on 4/16/2024 in #workers-help
TCP Socket does not seem to work on Worker
Just a small update. I was able to open https://connect.fifo.workers.dev/gopher/wbgopher from clicking the link in this thread, and see the response text. However, when I refresh the link, the same issue happens.
5 replies
CDCloudflare Developers
Created by Esente on 4/16/2024 in #workers-help
TCP Socket does not seem to work on Worker
The same example code on the TCP sockets page:
import { connect } from 'cloudflare:sockets';

export default {
async fetch(req: Request) {
const gopherAddr = { hostname: "gopher.floodgap.com", port: 70 };
const url = new URL(req.url);

try {
const socket = connect(gopherAddr);

const writer = socket.writable.getWriter()
const encoder = new TextEncoder();
const encoded = encoder.encode(url.pathname + "\r\n");
await writer.write(encoded);

return new Response(socket.readable, { headers: { "Content-Type": "text/plain" } });
} catch (error) {
return new Response("Socket connection failed: " + error, { status: 500 });
}
}
};
import { connect } from 'cloudflare:sockets';

export default {
async fetch(req: Request) {
const gopherAddr = { hostname: "gopher.floodgap.com", port: 70 };
const url = new URL(req.url);

try {
const socket = connect(gopherAddr);

const writer = socket.writable.getWriter()
const encoder = new TextEncoder();
const encoded = encoder.encode(url.pathname + "\r\n");
await writer.write(encoded);

return new Response(socket.readable, { headers: { "Content-Type": "text/plain" } });
} catch (error) {
return new Response("Socket connection failed: " + error, { status: 500 });
}
}
};
5 replies