func handleWss(wsconn *websocket.Conn) { p := Responder{nil, wsconn.Request(), time.Now()} serverIP := wsconn.Request().Header.Get("X-Server-IP") conn, err := net.Dial("tcp", serverIP) if err != nil { p.errorLog(http.StatusInternalServerError, "Error connecting to '%s': %s", serverIP, err.Error()) wsconn.Close() return } defer conn.Close() defer wsconn.Close() wsconn.PayloadType = websocket.BinaryFrame t := ©Syncer{alive: true} go p.CopyR2W(t, conn, wsconn, serverIP+" ws2vnc") go p.CopyR2W(t, wsconn, conn, serverIP+" vnc2ws") p.errorLog(http.StatusOK, "websocket started: '%s'", serverIP) for t.IsAlive() { time.Sleep(100 * time.Millisecond) } p.errorLog(http.StatusOK, "websocket closed: '%s'", serverIP) }
func (s *Server) Accept(ws *websocket.Conn) { ws.PayloadType = websocket.BinaryFrame if _, err := NewConnection(s, ws); err != nil { ws.SetDeadline(time.Now().Add(2 * time.Millisecond)) ws.Close() } }
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) }
func (c Console) Websocket(conn *websocket.Conn) { conn.PayloadType = websocket.BinaryFrame r := byteio.StickyReader{Reader: &byteio.LittleEndianReader{conn}} w := byteio.StickyWriter{Writer: &byteio.LittleEndianWriter{Writer: conn}} err := c.handle(&r, &w) if err != nil { writeError(&w, err) } }
func handle_ws(ws *websocket.Conn) { // Gob based RPC requires binary web socket frames. ws.PayloadType = websocket.BinaryFrame l := &Listener{} r := weeb.NewRpc(ws, l) l.rpc = r // go HelloRpcClient(r) err := r.Serve() if err != io.EOF { log.Printf("%v", err) } ws.Close() }
func echoHandler(ws *websocket.Conn) { fmt.Printf("Player joined : ", ws) defer ws.Close() x1 := uint16(0) y1 := uint16(0) x2 := uint16(1) y2 := uint16(1) ws.PayloadType = websocket.BinaryFrame clientData := make([]byte, 100) for { n, err := ws.Read(clientData) if err != nil { fmt.Printf("Error on stream ", err.Error()) ws.Close() return } s := string(clientData[:n]) //fmt.Printf("Received: %d bytes: %v (string is %v)\n", n, clientData[:n], s) switch s { case "D": x1++ case "Q": x1-- case "S": y1++ case "Z": y1-- } data := [4]uint16{x1, y1, x2, y2} data2 := toBytes(data) n, err = ws.Write(data2) } }