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

How to extract a fd using this library? #331

Open
kant777 opened this issue Feb 21, 2022 · 1 comment
Open

How to extract a fd using this library? #331

kant777 opened this issue Feb 21, 2022 · 1 comment
Milestone

Comments

@kant777
Copy link

kant777 commented Feb 21, 2022

I am trying to keep track of some user state in my application and I would like to get an integer fd of the connection so I can hash and keep track of it in my data structure. I tried below and I get the following error

reflect: call of reflect.Value.FieldByName on zero Value

having this functionality in the connection struct would really help.

netConn := websocket.NetConn(context.Background(), conn, websocket.MessageText)
func GetFdFromConn(conn net.Conn) int {
	tcpConn := reflect.Indirect(reflect.ValueOf(conn)).FieldByName("conn")
	fdVal := tcpConn.FieldByName("fd")
	pfdVal := reflect.Indirect(fdVal).FieldByName("pfd")
	return int(pfdVal.FieldByName("Sysfd").Int())
}
@kant777 kant777 closed this as completed Feb 21, 2022
@kant777 kant777 reopened this Feb 21, 2022
@nhooyr
Copy link
Owner

nhooyr commented Mar 5, 2023

Right now there is no way but I will improve the API to add one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants