Giter Site home page Giter Site logo

websocket's Introduction

About

A high-performance WebSocket library for the Go programming language with built-in retry logic and timeout protection.

This is free and unencumbered software released into the public domain.

API CI

Use

Most implementations will boot from an HTTP Upgrade.

http.HandleFunc("/echo", func(resp http.ResponseWriter, req *http.Request) {
	// client safety support
	originCheck := func(s string, o *httpws.Origin) (pass bool) {
		return o != nil && o.Host == "example.com" && o.Scheme == "https"
	}
	if !httpws.AllowOrigin(req, originCheck) {
		http.Error(w, "HTTP Origin rejected", http.StatusForbidden)
		return
	}

	// switch protocol to WebSocket
	conn, err := httpws.Upgrade(resp, req, nil, time.Second)
	if err != nil {
		return
	}
	defer conn.Close()

	// limit to standard types
	conn.Accept = websocket.AcceptV13

	var buf [2048]byte
	for {
		// read message
		opcode, n, err := conn.Receive(buf[:], time.Second, time.Minute)
		if err != nil {
			if _, ok := err.(websocket.ClosedError); !ok && err != io.EOF {
				log.Print("receive error: ", err)
			}
			return
		}

		// write message
		err = conn.Send(opcode, buf[:n], time.Second)
		if err != nil {
			if _, ok := err.(websocket.ClosedError); !ok {
				log.Print("send error: ", err)
			}
			return
		}
	}
})

Performance

The /tcp variants wire the raw messages to display WebSocket protocol overhead.

name               time/op
Receive/buffer-12   6.73µs ± 5%
Receive/stream-12   45.7µs ± 3%
Receive/tcp-12      6.28µs ± 3%
Send/buffer-12      10.0µs ± 1%
Send/stream-12      23.0µs ± 1%
Send/tcp-12         10.0µs ± 1%

name               speed
Receive/buffer-12  888MB/s ± 5%
Receive/stream-12  131MB/s ± 3%
Receive/tcp-12     951MB/s ± 3%
Send/buffer-12     598MB/s ± 1%
Send/stream-12     260MB/s ± 1%
Send/tcp-12        595MB/s ± 1%

name               alloc/op
Receive/buffer-12    0.00B     
Receive/stream-12    34.0B ± 0%
Receive/tcp-12       0.00B     
Send/buffer-12       0.00B     
Send/stream-12       32.0B ± 0%
Send/tcp-12          0.00B     

name               allocs/op
Receive/buffer-12     0.00     
Receive/stream-12     0.00     
Receive/tcp-12        0.00     
Send/buffer-12        0.00     
Send/stream-12        1.00 ± 0%
Send/tcp-12           0.00     

websocket's People

Contributors

pascaldekloe avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kirinse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.