<commit_before>package api_ws

import (
    "fmt"
    "net/http"
	"github.com/gorilla/websocket"
    "github.com/deze333/vroom/errors"
    "github.com/deze333/vroom/util"
)

//------------------------------------------------------------
// WebSocket Implementation
//------------------------------------------------------------

// Handles WS Not Authd response.
func Handle_NotAuthd(w http.ResponseWriter, r *http.Request) {

    http.Error(w, "Not authorized", http.StatusUnauthorized)
}

// Creates WS connecttion and processes it in forever loop.
func Handle(w http.ResponseWriter, r *http.Request, router *Router, isAuthd bool) {

    // Open websocket connection
    conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)

    // Process error
    if _, ok := err.(websocket.HandshakeError); ok {
        http.Error(w, "Not a websocket handshake", http.StatusBadRequest)
        return

    } else if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    ws := &Conn{
        isAuthd: isAuthd,
        router: router,
        conn: conn,
        chanClose: make(chan int),
        chanSend:  make(chan *Broadcast),
        isOpen:  true,
    }

    // Register authenticated connections
    RegisterConn(w, r, ws)

    // Close when done
    defer func() {
        DeregisterConn(ws)
        ws.conn.Close()
    }()

    fmt.Printf("++++ WebSocket %p opened\n", ws.conn)

    // Close connection channel

    // Start incoming/outgoing listeners
    go procIncoming(w, r, ws)
    go procOutgoing(w, r, ws)

    // Wait for close message
    <-ws.chanClose
    ws.chanSend <- CloseBroadcastMessage
    ws.isOpen = false

    fmt.Printf("---X WebSocket %p closed\n", ws.conn)
}

// Processor for incoming (passive) messages.
func procIncoming(w http.ResponseWriter, r *http.Request, ws *Conn) {

    var msgType int
    var msg []byte
    var err error

    // Forever loop
    for {

        // Read message
        msgType, msg, err = ws.conn.ReadMessage()
        if err != nil {
            break
        }

        switch msgType {

        // Text message
        case websocket.TextMessage:
            processMessage(w, r, msg, ws)

        // Binary not supported, bye
        case websocket.BinaryMessage:
            break

        case websocket.CloseMessage:
            break

        case websocket.PingMessage:

        case websocket.PongMessage:

        default:
        }
    }

    // Signal connection closed
    ws.chanClose <- 1
}

// Processor for outgoing (active) messages.
func procOutgoing(w http.ResponseWriter, r *http.Request, ws *Conn) {<commit_msg>ws message testing<commit_after>package api_ws

import (
    "fmt"
    "net/http"
	"github.com/gorilla/websocket"
    "github.com/deze333/vroom/errors"
    "github.com/deze333/vroom/util"
)

//------------------------------------------------------------
// WebSocket Implementation
//------------------------------------------------------------

// Handles WS Not Authd response.
func Handle_NotAuthd(w http.ResponseWriter, r *http.Request) {

    http.Error(w, "Not authorized", http.StatusUnauthorized)
}

// Creates WS connecttion and processes it in forever loop.
func Handle(w http.ResponseWriter, r *http.Request, router *Router, isAuthd bool) {

    // Open websocket connection
    conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)

    // Process error
    if _, ok := err.(websocket.HandshakeError); ok {
        http.Error(w, "Not a websocket handshake", http.StatusBadRequest)
        return

    } else if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    ws := &Conn{
        isAuthd: isAuthd,
        router: router,
        conn: conn,
        chanClose: make(chan int),
        chanSend:  make(chan *Broadcast),
        isOpen:  true,
    }

    // Register authenticated connections
    RegisterConn(w, r, ws)

    // Close when done
    defer func() {
        DeregisterConn(ws)
        ws.conn.Close()
    }()

    fmt.Printf("++++ WebSocket %p opened\n", ws.conn)

    // Close connection channel

    // Start incoming/outgoing listeners
    go procIncoming(w, r, ws)
    go procOutgoing(w, r, ws)

    // Wait for close message
    <-ws.chanClose
    ws.chanSend <- CloseBroadcastMessage
    ws.isOpen = false

    fmt.Printf("---X WebSocket %p closed\n", ws.conn)
}

// Processor for incoming (passive) messages.
func procIncoming(w http.ResponseWriter, r *http.Request, ws *Conn) {

    var msgType int
    var msg []byte
    var err error

    // Forever loop
    for {

        // Read message
        msgType, msg, err = ws.conn.ReadMessage()
        if err != nil {
            fmt.Printf("---? WebSocket %p error: %v\n", ws.conn, err)
            break
        }

        switch msgType {

        // Text message
        case websocket.TextMessage:
            processMessage(w, r, msg, ws)

        // Binary not supported, bye
        case websocket.BinaryMessage:
            fmt.Printf("---> WebSocket %p binary msg\n", ws.conn)

        case websocket.CloseMessage:
            fmt.Printf("---> WebSocket %p CLOSE msg\n", ws.conn)
            break

        case websocket.PingMessage:
            fmt.Printf("---> WebSocket %p ping msg\n", ws.conn)

        case websocket.PongMessage:
            fmt.Printf("---> WebSocket %p pong msg\n", ws.conn)

        default:
            fmt.Printf("---> WebSocket %p other msg: %v\n", ws.conn, msgType)
        }
    }

    // Signal connection closed
    ws.chanClose <- 1
}

// Processor for outgoing (active) messages.
func procOutgoing(w http.ResponseWriter, r *http.Request, ws *Conn) {
{'fraction_non_alphanumeric': 0.15539823008849557, 'fraction_numerical': 0.005309734513274336, 'mean_word_length': 1.8846350178662583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1827036', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1666, 'n_words': 462}