Web Socket output

Syntax to be used in Scoreboard OCR:

ws://host:port/[stream_id]
wss://host:port/[stream_id]
ws://localhost:15001/stream1

Command sent when creating new connection (assuming stream_id has been entered):

{"type":"register", "stream":"stream1", "role":"ocr"}

Command sent when updating data:

{“type”:”ocr”, “values”:{"field_name": “value”, "field_name2": “value”}}
{“type”:”ocr”, “values”:{"time": “3:12”, "score_a": “56”}}

Websocket server golang sample

In order to make this method work, you will need a websocket server. Here we have an example of golang code that you can use to create a websocket server:

package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/websocket"
)

func main() {
	http.HandleFunc("/", wsHandler)

	panic(http.ListenAndServe(":15001", nil))
}

func wsHandler(w http.ResponseWriter, r *http.Request) {
	conn, err := websocket.Upgrade(w, r, w.Header(), 1024, 1024)
	if err != nil {
		http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
	}

	go handleData(conn)
}

func handleData(conn *websocket.Conn) {
	defer conn.Close()
	for {
		_, b, err := conn.ReadMessage()

		if err != nil {
			fmt.Println("Error reading json.", err)
			break
		}

		fmt.Printf("Got message: %v\n", string(b))

		if string(b) == `{"type":"ping"}` {
			conn.WriteMessage(websocket.TextMessage, []byte(`{"type":"pong"}`))
		}
	}
}