Skip to content

Instantly share code, notes, and snippets.

@calmh
Created October 9, 2013 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calmh/6903711 to your computer and use it in GitHub Desktop.
Save calmh/6903711 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"net"
"sync"
"time"
)
func main() {
l, e := net.Listen("tcp", ":3994")
if e != nil {
panic(e)
}
for {
c, e := l.Accept()
if e != nil {
panic(e)
}
go func(c net.Conn) {
dec := json.NewDecoder(c)
enc := json.NewEncoder(c)
var encl sync.Mutex
for {
var r map[string]interface{}
dec.Decode(&r)
go func() {
time.Sleep(1 * time.Second)
resp := map[string]interface{}{
"jsonrpc": "2.0",
"result": nil,
"id": r["id"],
}
encl.Lock()
enc.Encode(resp)
encl.Unlock()
}()
}
}(c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment