Esempio n. 1
0
func handleWebSocket(conn *websocket.Conn) {
	respStatus := http.StatusOK
	defer func() {
		conn.Close()
		logRequest(HTTPS_WEBSOCKET, respStatus, conn.Request().Host, conn.Request())
	}()
	request := make([]byte, 1024)
	for {
		n, err := conn.Read(request)
		if err != nil {
			if debugMode {
				log.Error("Error reading on WebSocket: %s", err)
			}
			break
		}
		response, status := getLiveItems(string(request[:n]))
		if status != http.StatusOK {
			respStatus = status
			break
		}
		if len(response) != 0 {
			if _, err = conn.Write(response); err != nil {
				break
			}
		}
	}
}
Esempio n. 2
0
func clientHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	for {
		buf := make([]byte, 128)
		n, err := ws.Read(buf)
		if err != nil {
			log.Print("Reading Buffer: ", err)
			break
		}

		var m message
		err = json.Unmarshal(buf[0:n], &m)
		if err != nil {
			log.Print("Parsing JSON: ", buf, m, err)
			break
		}

		messageChan <- message{m.Text, m.Id, m.User, m.Command}
	}
}
Esempio n. 3
0
/*
 * Get messages from client
 */
func clientHandler(ws *websocket.Conn) {
	defer func() {
		log.Println("Client handler closed.")
		ws.Close()
	}()

	buf := make([]byte, 256)
	stopCh := make(chan bool)
	var input ProcessInput
	for {
		n, err := ws.Read(buf)
		if err != nil {
			break
		}

		// get data
		err = json.Unmarshal(buf[0:n], &input)
		if err != nil {
			stopCh <- true
			break
		}

		workChan <- Work{ws, &input, stopCh}
	}
}
Esempio n. 4
0
func IsClosed(ws *websocket.Conn) bool {
	var ibuff [50]byte

	_, err := ws.Read(ibuff[0:50])
	if err != nil {
		return true
	}

	return false
}
Esempio n. 5
0
func ReadThread(ws *websocket.Conn, msg []byte, ch chan int) {
	for {
		n, error := ws.Read(msg)
		if error != nil {
			fmt.Println("WebSocket read error: ", error)
			ch <- 0
			break
		}
		ch <- n
		if n == 0 {
			break
		}
	}
}
Esempio n. 6
0
func sub(ws *websocket.Conn) {
	println("Beginning Sub")

	var resp = make([]byte, 512)
	for {
		println("Waiting Read")
		_, err := ws.Read(resp)

		if err != nil {
			panic(err)
		}
		// fmt.Println("Received 1:", string(resp[0:n]))
		// fmt.Println("Received %v:", n)
		ch <- 1
	}
}
Esempio n. 7
0
func clientHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	buf := make([]byte, 256)
	for {
		n, err := ws.Read(buf)
		if err != nil {
			break
		}
		messageChan <- buf[0:n]
	}
}
Esempio n. 8
0
func indexHandler(ws *websocket.Conn) {
	var ibuff [50]byte

	i, err := ws.Read(ibuff[0:50])
	if err != nil {
		fmt.Printf("WebSocket error: " + err.String())
		return
	}

	msg := string(ibuff[0:i])
	idx := strings.Index(msg, "!")
	if idx < 0 {
		fmt.Printf("Salt not found\n")
		return
	}

	idstr := msg[0:idx]
	salt := msg[idx+1:]

	uid, err := strconv.Atoui64(idstr)
	if err != nil {
		fmt.Printf("User ID invalid\n")
		return
	}

	defer UnlinkUser(uid, ws)

	usr := LinkUser(uid, salt, ws)
	if usr == nil {
		fmt.Printf("Cannot link with User ID\n")
		return
	}

	for usr.exit == false {
		time.Sleep(1e8)

		if IsClosed(ws) {
			break
		}
	}
}
Esempio n. 9
0
func doEventStream(ws *websocket.Conn) {
	ID := chat.UUID()
	defer func() {
		subscriptionChan <- &Subscription{ws, chat.REMOVE_CONNECTION, &User{ID, nil, ""}}
		ws.Close()
	}()

	subscriptionChan <- &Subscription{ws, chat.ADD_CONNECTION, &User{ID, nil, ""}}
	newStartMessage, _ := json.Marshal(
		MessageData{
			"start",
			map[string]interface{}{
				"ID": ID,
			}})

	ws.Write(newStartMessage)

	for {
		buf := make([]byte, 1024)

		n, err := ws.Read(buf)
		if err != nil {
			log.Println("Error reading from websocket connection: ", err.Error())
			break
		}

		newMessageData := new(MessageData)
		err = json.Unmarshal(buf[0:n], newMessageData)

		if err != nil {
			log.Println("Error unmarshaling message: ", string(buf[0:n]), " : ", err.Error())
		}

		messageChan <- message{
			ws,
			*newMessageData,
		}
	}
}
Esempio n. 10
0
func broadcastHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
		fmt.Printf("Closed \n")
	}()

	fmt.Printf("Adding to subscription \n")

	subscriptionChan <- subscription{ws, true}
	fmt.Printf("Added to subscription \n")

	buf := make([]byte, 256)
	for {
		n, err := ws.Read(buf)
		fmt.Printf("Reding message %v \n", n)

		if err != nil {
			break
		}
		messageChan <- buf[0:n]
	}
}
Esempio n. 11
0
func doEventStream(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	for {
		buf := make([]byte, 512)

		n, err := ws.Read(buf)
		if err != nil {
			log.Println("Error reading from websocket connection")
			break
		}

		messageChan <- message{
			ws,
			buf[0:n],
		}
	}
}
Esempio n. 12
0
func wssServe(ws *websocket.Conn) {
	fmt.Println("Socket connect")
	ws.Write([]byte(strconv.Itoa(int(*state))))
	b := make([]byte, 2)
	var quit bool
	for !quit {
		ws.Read(b)
		t, _ := strconv.Atoi(string(b))
		fmt.Println(t)
		switch t {
		case 0:
			func() {
				fmt.Println("Server Init")
				if *state > 0 {
					ws.Write([]byte(strconv.Itoa(int(*state))))
				} else {
					ws.Write([]byte("0"))
					b = make([]byte, 5)
					ws.Read(b)
					world, _ := strconv.Atob(string(b))
					fmt.Println(world)
					if world {

					} else {

					}
				}
			}()
		default:
			func() {
				fmt.Println(string(b))
				quit = true
			}()
		}
		quit = true
	}
}