After learning the foundation of HTTP, naturally came to the WebSocket protocol, just two days ago in order to compare the performance of Java and Go two languages WebSocket script, and practice again, feel that I have already done, specially to write an article to record.

Let’s review the Java and Python test articles on sockets, websockets, and socket. IO.

  • Socket interface development and testing
  • Client encapsulation based on WebSocket
  • Client encapsulation based on socket. IO
  • Socket interface fixed QPS performance test practice
  • Test practice of white board dot matrix coordinate transmission interface based on socket. IO
  • Socket interface asynchronous authentication practice
  • Socket.IO interface multi-user testing practice
  • Python socket. IO interface test script

Let’s share the development and test of WebSocket interface with Go language.

Rely on

This article uses the /net/websocket library, as well as another/Gorilla /websocket that I’ll share when I’m fully comfortable with. One of the biggest benefits of the Go library is that the same library can both do interface development and send requests as a client.

WebSocket development

This place has a big hole, a lot of the information online is out of date, it doesn’t actually work. The reason is that /net/websocket is relatively less used to do the development of the server side. There is an error article copied by the whole network, which greatly affects the experience of self-learning small white.

The function of the server is simple. The function design is as follows: After receiving the message from the client, the message is returned as the current time.

// Echo
// @description: Handle the WebSocket interface
// @param ws
func Echo(ws *websocket.Conn) {
	var err error
	for {
		var reply string
		iferr = websocket.Message.Receive(ws, &reply); err ! =nil {
			fmt.Println("receive failed:", err)
			break
		}
		log.Printf("Received message :%s", reply)
		msg := string(time.Now().String())
		websocket.Message.Send(ws, msg)
	}

}
// TestSer
// @description: Creates a WebSocket interface
// @param t
func TestSer(t *testing.T) {
	// Accept the routing address of webSocket
	http.HandleFunc("/websocket".func(w http.ResponseWriter, req *http.Request) {
		s := websocket.Server{Handler: websocket.Handler(Echo)}
		s.ServeHTTP(w, req)
	})
	if err := http.ListenAndServe(": 1234".nil); err ! =nil {

		log.Fatal("ListenAndServe:", err)

	}
}

Copy the code

The big pitfall here is the need to transform the WebSocket handle into the HTTP Handle, which is the upgrader of my personal understanding.

The client

Here we simply send a message to the server and print the message returned by the server. Because the server logic is simple, so is the client. If you need to test WebSocket in combination with Chan in the actual test, it is very useful.

// TestWebSocket
// @description: Tests the WebSocket script
// @param t
func TestWebSocket(t *testing.T) {

	url := "wss://wspri.coinall.ltd:8443/ws/v5/public"
	c, res, err := websocket.DefaultDialer.Dial(url, nil)
	iferr ! =nil {
		log.Fatal("Connection failed :", err)
	}
	log.Printf(Response: "% s", fmt.Sprint(res))
	defer c.Close()
	done := make(chan struct{})
	err = c.WriteMessage(websocket.TextMessage, []byte("Hi, I'm FunTester."))
	iferr ! =nil {
		fmt.Println(err)
	}
	go func(a) {
		defer close(done)
		for {
			_, message, err := c.ReadMessage()
			iferr ! =nil {
				log.Fatal(err)
				break
			}
			log.Printf("Received message: %s", message)

		}
	}()
	s := <-done
	fmt.Println(s)

}

Copy the code

test

Start the server first, and then start the client. Go language is also much faster than Java, which has obvious advantages.

I also wrote a Java test client:

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("Hi, I'm Funtester-Java,Have Fun ~ Tester!"); }}Copy the code

Server logs:

=== RUN TestSer 2021/11/09 18:03:20 hello, I am funtester-go,Have Fun ~ Tester! 2021/11/09 18:05:49 Received: Hello, I am Funtester-Java,Have Fun ~ Tester!Copy the code

Go client logs:

=== RUN   TestMn2
2021-11-09 18:03:20.570277 +0800 CST m=+11.854257683

Copy the code

Java client logging:

INFO->Main current user: oker, working directory: / Users/oker/IdeaProjects funtester/system coding format: utf-8, Mac OS X system version: 10.16
INFO->Main FunTester_0 Start connection...
INFO->WebSocketConnectReadThread - 14 FunTester_0 is to establish a socket connection...
INFO->WebSocketConnectReadThread - 14 handshake key information: Connection, value: the Upgrade
INFO->WebSocketConnectReadThread shaking hands - 14 key information: Sec - WebSocket - Accept and value: mpWx5ntxvsNp75f8ubGZPmjSrn0 =
INFO->WebSocketConnectReadThread - 14 handshake key information: Upgrade, value: websocket
INFO->Main FunTester_0 connection successful!
INFO->WebSocketConnectReadThread - 14 FunTester_0 received: 2021-11-09 18:05:49. 374599 + 0800 CST m = + 43.517144103

Copy the code

Have Fun ~ Tester! Welcome to FunTester

  • 140 interview questions (UI, Linux, MySQL, API, security)
  • Graphic HTTP brain map
  • Share a Fiddler study bag
  • Thread pools handle batch interface request practices
  • 100K QPS, K6, Gatling and FunTester showdown!
  • Performance bottleneck tuning
  • How do I choose an API test tool
  • Beginner’s API testing tips
  • API Automation Test Guide
  • API Testing Basics
  • There is no data to drive automated tests
  • Automated and manual testing to keep the balance!
  • 43 common software test categories