// handle implements a WebSocket handler. func (r *Reader) handle(ws *websocket.Conn) { encode := len(ws.Config().Protocol) > 0 && ws.Config().Protocol[0] == base64BinaryWebSocketProtocol defer close(r.err) defer ws.Close() go IgnoreReceives(ws, r.timeout) r.err <- messageCopy(ws, r.r, encode, r.ping, r.timeout) }
func (conn *Conn) initialize(ws *websocket.Conn) { negotiated := ws.Config().Protocol conn.selectedProtocol = negotiated[0] p := conn.protocols[conn.selectedProtocol] if p.Binary { conn.codec = rawCodec } else { conn.codec = base64Codec } conn.ws = ws conn.channels = make([]*websocketChannel, len(p.Channels)) for i, t := range p.Channels { switch t { case ReadChannel: conn.channels[i] = newWebsocketChannel(conn, byte(i), true, false) case WriteChannel: conn.channels[i] = newWebsocketChannel(conn, byte(i), false, true) case ReadWriteChannel: conn.channels[i] = newWebsocketChannel(conn, byte(i), true, true) case IgnoreChannel: conn.channels[i] = newWebsocketChannel(conn, byte(i), false, false) } } close(conn.ready) }
func vncWsHandler(ws *websocket.Conn) { // URL should be of the form `/ws/<vm_name>` path := strings.Trim(ws.Config().Location.Path, "/") fields := strings.Split(path, "/") if len(fields) != 2 { return } vmName := fields[1] vms := GlobalVMs() vm, err := vms.findKvmVM(vmName) if err != nil { log.Errorln(err) return } // Undocumented "feature" of websocket -- need to set to PayloadType in // order for a direct io.Copy to work. ws.PayloadType = websocket.BinaryFrame // connect to the remote host rhost := fmt.Sprintf("%v:%v", vm.Host, vm.VNCPort) remote, err := net.Dial("tcp", rhost) if err != nil { log.Errorln(err) return } defer remote.Close() go io.Copy(ws, remote) io.Copy(remote, ws) log.Info("ws client disconnected from %v", rhost) }
// handle implements a WebSocket handler. func (r *Reader) handle(ws *websocket.Conn) { // Close the connection when the client requests it, or when we finish streaming, whichever happens first closeConnOnce := &sync.Once{} closeConn := func() { closeConnOnce.Do(func() { ws.Close() }) } negotiated := ws.Config().Protocol r.selectedProtocol = negotiated[0] defer close(r.err) defer closeConn() go func() { defer runtime.HandleCrash() // This blocks until the connection is closed. // Client should not send anything. IgnoreReceives(ws, r.timeout) // Once the client closes, we should also close closeConn() }() r.err <- messageCopy(ws, r.r, !r.protocols[r.selectedProtocol].Binary, r.ping, r.timeout) }
func handleStatistic(ws *websocket.Conn) { for k, v := range ws.Config().Header { log.Debug.Println(k, " - ", v) } ws.Write([]byte("Hi")) }
// WSCompileRunHandler handles the websocket connection for a given compile/run action. // It handles transcoding Messages to and from JSON format, and handles starting // and killing processes. func WSCompileRunHandler(c *websocket.Conn) { c.Config() in, out := make(chan *Message), make(chan *Message) errc := make(chan error, 1) // Decode messages from client and send to the in channel. go func() { dec := json.NewDecoder(c) for { var m Message if err := dec.Decode(&m); err != nil { fmt.Printf("error in dec.Decode %s\n", err) errc <- err return } in <- &m } }() // Receive messages from the out channel and encode to the client. go func() { enc := json.NewEncoder(c) for m := range out { if err := enc.Encode(m); err != nil { fmt.Printf("error in enc.Encode %s\n", err) errc <- err return } } }() // Start and kill Processes and handle errors. proc := make(map[string]*Process) for { select { case m := <-in: switch m.Kind { case "run": goTest := m.GoTest proc[m.Id].Kill() if goTest { proc[m.Id] = StartTest(m.Id, m.SrcDir, m.RunOpts, nil, out) } else { proc[m.Id] = StartProcess(m.Id, m.Body, m.BuildOpts, m.RunOpts, nil, out) } case "kill": proc[m.Id].Kill() } case err := <-errc: // A encode or decode has failed; bail. log.Println(err) // Shut down any running processes. for _, p := range proc { p.Kill() } return } } }
func (conn *Conn) initialize(ws *websocket.Conn) { protocols := ws.Config().Protocol switch { case len(protocols) == 0, protocols[0] == channelWebSocketProtocol: conn.codec = rawCodec case protocols[0] == base64ChannelWebSocketProtocol: conn.codec = base64Codec } conn.ws = ws close(conn.ready) }
// jsonServer echoes back json string sent from client using websocket.JSON. func jsonServer(ws *websocket.Conn) { fmt.Printf("jsonServer %#v\n", ws.Config()) for { var msg T // Receive receives a text message serialized T as JSON. err := websocket.JSON.Receive(ws, &msg) if err != nil { fmt.Println(err) break } fmt.Printf("recv:%#v\n", msg) // Send send a text message serialized T as JSON. err = websocket.JSON.Send(ws, msg) if err != nil { fmt.Println(err) break } fmt.Printf("send:%#v\n", msg) } }
// readWriteServer echoes back messages sent from client using Read and Write. func readWriteServer(ws *websocket.Conn) { fmt.Printf("readWriteServer %#v\n", ws.Config()) for { buf := make([]byte, 100) // Read at most 100 bytes. If client sends a message more than // 100 bytes, first Read just reads first 100 bytes. // Next Read will read next at most 100 bytes. n, err := ws.Read(buf) if err != nil { fmt.Println(err) break } fmt.Printf("recv:%q\n", buf[:n]) // Write send a message to the client. n, err = ws.Write(buf[:n]) if err != nil { fmt.Println(err) break } fmt.Printf("send:%q\n", buf[:n]) } fmt.Println("readWriteServer finished") }
// handle implements a WebSocket handler. func (r *Reader) handle(ws *websocket.Conn) { // Close the connection when the client requests it, or when we finish streaming, whichever happens first closeConnOnce := &sync.Once{} closeConn := func() { closeConnOnce.Do(func() { ws.Close() }) } encode := len(ws.Config().Protocol) > 0 && ws.Config().Protocol[0] == base64BinaryWebSocketProtocol defer close(r.err) defer closeConn() go func() { defer runtime.HandleCrash() // This blocks until the connection is closed. // Client should not send anything. IgnoreReceives(ws, r.timeout) // Once the client closes, we should also close closeConn() }() r.err <- messageCopy(ws, r.r, encode, r.ping, r.timeout) }
func (e *echoHandler) serveWS(conn *websocket.Conn) { c := conn.Config() r := conn.Request() h := map[string]string{} for k, _ := range r.Header { h[k] = r.Header.Get(k) } b, _ := json.MarshalIndent(&struct { Location string Origin string Protocol []string Version int Headers map[string]string }{ Location: c.Location.String(), Origin: c.Origin.String(), Protocol: c.Protocol, Version: c.Version, Headers: h, }, "", " ") conn.Write(b) io.Copy(conn, conn) conn.Close() }
// copyServer echoes back messages sent from client using io.Copy. func copyServer(ws *websocket.Conn) { fmt.Printf("copyServer %#v\n", ws.Config()) io.Copy(ws, ws) fmt.Println("copyServer finished") }