Giter Site home page Giter Site logo

xgfone / go-websocket Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 3.0 87 KB

Another websocket implementation, which is inspired by https://github.com/noVNC/websockify and https://github.com/gorilla/websocket.

License: Apache License 2.0

Go 100.00%
websocket websocket-server websocket-client websocket-library novnc vnc vnc-proxy websocket-vnc go go-websocket golang proxy ws

go-websocket's Introduction

websocket GoDoc License

This is a websocket implementation supporting Go1.7+, which is inspired by websockify and websocket.

Difference from gorilla/websocket

  • gorilla/websocket only has a goroutine to read from websocket and a goroutine to write to websocket, that's, read or write can be done concurrently.
  • Though this library cannot read from websocket concurrently, it's able to write to websocket concurrently. Moreover, it will be enhanced to read concurrently.

Install

$ go get -u github.com/xgfone/go-websocket

VNC Proxy on WebSocket

The sub-package vncproxy provides a HTTP handler about VNC Proxy on Websocket.

Example

Websocket Server Example

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/xgfone/go-websocket"
)

var upgrader = websocket.Upgrader{
	MaxMsgSize:  1024,
	Timeout:     time.Second * 30,
	CheckOrigin: func(r *http.Request) bool { return true },
}

func websocketHandler(rw http.ResponseWriter, r *http.Request) {
	ws, err := upgrader.Upgrade(rw, r, nil)
	if err != nil {
		log.Printf("failed to upgrade to websocket: %s\n", err)
		return
	}

	ws.Run(func(msgType int, message []byte) {
		switch msgType {
		case websocket.MsgTypeBinary:
			ws.SendBinaryMsg(message)
		case websocket.MsgTypeText:
			ws.SendTextMsg(message)
		}
	})
}

func main() {
	http.ListenAndServe(":80", http.HandlerFunc(websocketHandler))
}

Websocket Client Example

package main

import (
	"fmt"
	"time"

	"github.com/xgfone/go-websocket"
)

func main() {
	ws, err := websocket.NewClientWebsocket("ws://127.0.0.1/")
	if err == nil {
		go func() {
			tick := time.NewTicker(time.Second * 10)
			defer tick.Stop()
			for {
				select {
				case now := <-tick.C:
					if err := ws.SendTextMsg([]byte(now.String())); err != nil {
						fmt.Println(err)
						return
					}
				}
			}
		}()

		err = ws.Run(func(msgType int, msg []byte) {
			fmt.Printf("Receive: %s\n", string(msg))
		})
	}
	fmt.Println(err)
}

The client will output like this:

Receive: 2019-07-13 18:33:47.951688 +0800 CST m=+10.007139340
Receive: 2019-07-13 18:33:57.951479 +0800 CST m=+20.006605995
Receive: 2019-07-13 18:34:07.948628 +0800 CST m=+30.003442484
Receive: 2019-07-13 18:34:17.949763 +0800 CST m=+40.004270178
Receive: 2019-07-13 18:34:27.947877 +0800 CST m=+50.002081112
Receive: 2019-07-13 18:34:37.949986 +0800 CST m=+60.003888082
...

go-websocket's People

Contributors

xgfone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.