Skip to content

Instantly share code, notes, and snippets.

@bingo347
Created January 10, 2017 17:14
Show Gist options
  • Save bingo347/cbee13f11148097c278db3bfe4bc0184 to your computer and use it in GitHub Desktop.
Save bingo347/cbee13f11148097c278db3bfe4bc0184 to your computer and use it in GitHub Desktop.
send file descriptor from golang to nodejs
package main
import (
"net"
"fmt"
"syscall"
)
func main() {
addr, err := net.ResolveTCPAddr("tcp", "localhost:8090")
if err != nil {
fmt.Print(err)
return;
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
fmt.Print(err)
return;
}
defer conn.Close();
fmt.Fprintf(conn, "GET / HTTP/1.1\r\nHost: localhost:8090\r\nConnection: keep-alive\r\n\r\n")
file, err := conn.File()
if err != nil {
fmt.Print(err)
return;
}
fd := file.Fd()
rights := syscall.UnixRights(int(fd))
_, err = syscall.Write(3, []byte("{\"cmd\":\"NODE_HANDLE\",\"type\":\"net.Socket\",\"msg\":{\"test\":true}}\n"))
if err != nil {
fmt.Print(err)
return;
}
err = syscall.Sendmsg(3, nil, rights, nil, 0)
if err != nil {
fmt.Print(err)
return;
}
fmt.Print("ok\n")
for _, v := range syscall.Environ() {
fmt.Print(v, "\n")
}
}
@nareix
Copy link

nareix commented Apr 8, 2020

great job! thanks a lot!

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