Esempio n. 1
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)
	}
}
Esempio n. 2
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
		}
	}
}
Esempio n. 3
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)
	}
}
Esempio n. 4
0
func serveIndex(w http.ResponseWriter, r *http.Request) {
	// check for and process static file requests
	matches := static_regex.FindStringSubmatch(r.URL.Path)
	if len(matches) > 0 {
		static_http.ServeHTTP(w, r)
		logger.Debugf("Static fetch for resource: %s", r.URL.Path)
	} else {
		// return the index.html
		t, err := template.ParseFiles("index.html")
		if err != nil {
			panic(err)
		}
		t.Execute(w, "")
		logger.Debug("Fetch for home page")
	}
}