GithubHelp home page GithubHelp logo

flytomelight / wserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alfred-zhong/wserver

1.0 0.0 0.0 86 KB

Package for setup websocket server with message push

License: MIT License

Go 91.27% HTML 8.73%

wserver's Introduction

wserver

Build Status GoDoc Go Report Card

Package for setup websocket server with message push

This package is still not stable and not recommended to be used in production.

Basic Usage

Try to start a wserver, you just need to write like this.

Start wserver

server := wserver.NewServer(":12345")
if err := server.ListenAndServe(); err != nil {
    panic(err)
}

Now wserver listens on port: 12345.

Browser connecting

Now browser can connect to ws://ip:12345/ws. After connection established, browser should send a message to register. Register message looks like this.

{
    "token": "03A3408D-3BD4-4C6C-BDC7-8596E6D31848",
    "event": "whatever-it-interested"
}

The token is used for identification and event means what kind of messages the client interested (Like topic in MQ).

Push messages

Now you can send a request to http:/ip:12345/push to push a message. Message should look like this.

{
    "userId": "03A3408D-3BD4-4C6C-BDC7-8596E6D31848",
    "event": "whatever-it-interested",
    "message": "Hello World"
}

The userId is equal to token is not specified (customize by using server.AuthToken). The event is equal to that above and message is the real content will be sent to each websocket connection.

Example

The server code:

func main() {
	server := wserver.NewServer(":12345")

	// Define websocket connect url, default "/ws"
	server.WSPath = "/ws"
	// Define push message url, default "/push"
	server.PushPath = "/push"

	// Set AuthToken func to authorize websocket connection, token is sent by
	// client for registe.
	server.AuthToken = func(token string) (userID string, ok bool) {
		// TODO: check if token is valid and calculate userID
		if token == "aaa" {
			return "jack", true
		}

		return "", false
	}

	// Set PushAuth func to check push request. If the request is valid, returns
	// true. Otherwise return false and request will be ignored.
	server.PushAuth = func(r *http.Request) bool {
		// TODO: check if request is valid

		return true
	}

	// Run server
	if err := server.ListenAndServe(); err != nil {
		panic(err)
	}
}

If you want to run a demo. Then follow the steps below:

  • Go to _examples/server and run go run main.go to start a wserver.
  • Open the webpage _examples/index.html in the browser.
  • Go to _examples/push and run go run main.go to send some messages.

If all success, you will see the content like this:

demo-success

PS

Package is still not stable and I will improve it then.

PRs will be welcomed. ๐Ÿบ

wserver's People

Contributors

alfred-zhong avatar

Stargazers

 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.