init go event proxy
This commit is contained in:
parent
c99f619fe5
commit
dc206625bf
1 changed files with 52 additions and 0 deletions
52
event_proxy/main.go
Normal file
52
event_proxy/main.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HOST = "0.0.0.0"
|
||||||
|
PORT = "8080"
|
||||||
|
TYPE = "tcp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
listen, err := net.Listen(TYPE, HOST+":"+PORT)
|
||||||
|
println("Listening on", HOST+":"+PORT)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
// close listener
|
||||||
|
defer listen.Close()
|
||||||
|
for {
|
||||||
|
conn, err := listen.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
go handleRequest(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRequest(conn net.Conn) {
|
||||||
|
// incoming request
|
||||||
|
buffer := make([]byte, 1024)
|
||||||
|
_, err := conn.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// write data to response
|
||||||
|
time := time.Now().Format(time.ANSIC)
|
||||||
|
fmt.Printf("Message recived: %v. Received time: %v", string(buffer[:]), time)
|
||||||
|
conn.Write([]byte("1"))
|
||||||
|
// conn.Write([]byte(responseStr))
|
||||||
|
|
||||||
|
// close conn
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue