websocket icon indicating copy to clipboard operation
websocket copied to clipboard

How to get file descriptor from NetConn for EPOLL handling?

Open StarpTech opened this issue 1 year ago • 4 comments

Hi, as titled. We are trying to get the raw TCP connection to read the file descriptor.

netConn := websocket.NetConn(reqCtx.Context(), wsConn, websocket.MessageText)

We want to use this library in combination with https://github.com/smallnest/epoller

StarpTech avatar Oct 19 '24 11:10 StarpTech

Our workaround

	var netConn net.Conn

	clientTrace := &httptrace.ClientTrace{
		GotConn: func(info httptrace.GotConnInfo) {
			netConn = info.Conn
		},
	}
	clientTraceCtx := httptrace.WithClientTrace(requestContext, clientTrace)
	conn, upgradeResponse, err := websocket.Dial(clientTraceCtx, options.URL, &websocket.DialOptions{
		HTTPClient:      c.httpClient,
		HTTPHeader:      options.Header,
		CompressionMode: websocket.CompressionDisabled,
		Subprotocols:    subProtocols,
	})
	if err != nil {
		return nil, err
	}

StarpTech avatar Oct 20 '24 10:10 StarpTech

Thanks for sharing your workaround @StarpTech! We'll think about the API but surely we can expose an easier way to grab the raw net.Conn.

Somewhat related: #331

mafredri avatar Nov 14 '24 15:11 mafredri

@mafredri thanks and that would be great!

StarpTech avatar Nov 14 '24 15:11 StarpTech

Thanks for sharing the workaround @StarpTech, needed the Conn for something else and this made the trick.

Did you manage to use epoller with the library in the end? I'm curious to learn if it works and roughly how it improve things.

comunidadio avatar Mar 25 '25 04:03 comunidadio