Example #1
0
func (p *proxy) handleWebsocket(message []byte, cconn *clientConnection) {
	logger.Infof("Proxying message from client: %s", message)
	_, err := p.server.conn.Write(message)
	if err != nil {
		logger.Errorf("Error while writing to socket: %s", err)
	}
}
Example #2
0
func (pc *clientConnection) wsReadPump() {
	defer func() {
		pc.ws.Close()
	}()
	pc.ws.SetReadLimit(RECV_BUF_LEN)
	for {
		_, msg, err := pc.ws.ReadMessage()
		if err != nil {
			if err == io.EOF || err == io.ErrUnexpectedEOF {
				// the client ID here is redundant...
				killcconn := fmt.Sprintf("killClient:{\"Id\": %d}", pc.info.Id)
				pc.currentHandler.handleWebsocket([]byte(killcconn), pc)
			} else {
				logger.Errorf("Error while reading from websocket: %s", err)
			}
			break
		}
		logger.Debugf("Received %s from websocket", msg)
		select {
		case newHandler := <-pc.handlers:
			pc.currentHandler = newHandler
		default:
		}
		pc.currentHandler.handleWebsocket(msg, pc)
	}
}
Example #3
0
func (pc *clientConnection) wsWritePump() {
	for msg := range pc.toClient {
		logger.Debugf("Writing %s to websocket", msg)
		err := pc.ws.WriteMessage(websocket.TextMessage, msg)
		if err != nil {
			logger.Errorf("Error while writing to websocket: %s", err)
			break
		}
	}
}
Example #4
0
func (gh *game_hub) handleWebsocket(message []byte, cconn *clientConnection) {
	cmds := strings.SplitN(string(message), ":", 2)
	if len(cmds) == 2 {
		if fun, ok := gh.localHandlers[cmds[0]]; ok {
			fun(cmds[1], cconn)
		} else {
			logger.Warnf("Unrecognized command: %s", cmds[0])
			cconn.toClient <- []byte("unrecognized:")
		}
	} else {
		logger.Errorf("Malformed command: %s", cmds)
	}
}
Example #5
0
func (p *proxy) serverReadPump() {
	defer func() {
		p.server.conn.Close()
	}()
	for {
		buf := make([]byte, RECV_BUF_LEN)
		_, err := p.server.conn.Read(buf)
		if err != nil {
			logger.Errorf("Error while reading from socket: %s", err)
			break
		}
		logger.Debugf("Received %s from socket", buf)
		p.broadcast(buf)
	}
}
Example #6
0
func serveWs(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}
	if r.Header.Get("Origin") != "http://"+r.Host {
		http.Error(w, "Origin not allowed", 403)
		return
	}
	ws, err := websocket.Upgrade(w, r, nil, 1024, 1024)
	if _, ok := err.(websocket.HandshakeError); ok {
		http.Error(w, "Not a websocket handshake", 400)
		return
	} else if err != nil {
		logger.Errorf("Websocket upgrade error: %s", err)
		return
	}
	gamehub.wsRegister <- ws
}