Created
January 10, 2017 17:14
-
-
Save bingo347/cbee13f11148097c278db3bfe4bc0184 to your computer and use it in GitHub Desktop.
send file descriptor from golang to nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great job! thanks a lot!