Master Go WebSocket with Gorilla: Build, Test, and Compare Clients

This guide walks through using the Gorilla WebSocket library in Go, covering dependency setup, server implementation with an Upgrader, Java and Go client examples, testing procedures, and performance observations that highlight Go's advantages for WebSocket workloads.

FunTester
FunTester
FunTester
Master Go WebSocket with Gorilla: Build, Test, and Compare Clients

The article introduces the Gorilla WebSocket library, a widely adopted, high‑performance Go implementation that fills functional gaps left by the standard library.

Dependency

Add the library to your project:

github.com/gorilla/websocket v1.4.2

WebSocket Server Development

Unlike the standard library, Gorilla requires an Upgrader to convert an HTTP request into a WebSocket connection. The following snippet creates a simple upgrader:

var upgrader = websocket.Upgrader{
    ReadBufferSize:   1024,
    WriteBufferSize:  1024,
    HandshakeTimeout: 5 * time.Second,
}

A complete server handler reads messages, echoes them back, and logs activity:

func TestWEBs(t *testing.T) {
    upgrader := websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024, HandshakeTimeout: 5 * time.Second}
    http.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) {
        conn, _ := upgrader.Upgrade(w, r, nil)
        for {
            msgType, msg, err := conn.ReadMessage()
            if err != nil { return }
            fmt.Printf("%s receive: %s
", conn.RemoteAddr(), string(msg))
            if err = conn.WriteMessage(msgType, msg); err != nil { return }
        }
    })
    http.ListenAndServe(":12345", nil)
}

Client Implementations

Two client examples are provided: a Java client using a custom WebSocketFunClient wrapper, and a Go client that dials the server directly.

package com.funtest.javatest;
import com.funtester.frame.SourceCode;
import com.funtester.socket.WebSocketFunClient;
public class WebSocketT extends SourceCode {
    public static void main(String[] args) {
        WebSocketFunClient instance = WebSocketFunClient.getInstance("ws://localhost:1234/websocket");
        instance.connect();
        instance.send("你好,我是FunTester - Java ,Have Fun ~ Tester !");
    }
}
// TestWebSocket – Go client
func TestWebSocket(t *testing.T) {
    url := "ws://localhost:1234/websocket"
    c, res, err := websocket.DefaultDialer.Dial(url, nil)
    if err != nil { log.Fatal("连接失败:", err) }
    log.Printf("响应:%s", fmt.Sprint(res))
    defer c.Close()
    done := make(chan struct{})
    err = c.WriteMessage(websocket.TextMessage, []byte("你好,我是FunTester"))
    if err != nil { fmt.Println(err) }
    for {
        _, message, err := c.ReadMessage()
        if err != nil { log.Fatal(err); break }
        log.Printf("收到消息: %s", message)
    }
    <-done
}

Testing

Run the server first, then launch the clients. Sample logs show successful message exchange:

=== RUN   TestWEBs
[::1]:59843 sent: 你好,我是FunTester
127.0.0.1:59902 receive: 你好,我是FunTester - Java ,Have Fun ~ Tester !
=== RUN   TestWebSocket
2021/11/15 11:47:26 响应:&{101 Switching Protocols ...}
2021/11/15 11:47:26 收到消息: 你好,我是FunTester

Conclusion

The author notes that Go’s WebSocket implementation outperforms Java in connection throughput, handling tens of thousands of connections within minutes, making Go a preferred choice for future WebSocket functionality and performance testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendtestingGoWebSocketclient-servergorilla/websocket
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.