GithubHelp home page GithubHelp logo

aglyzov / ws-machine Goto Github PK

View Code? Open in Web Editor NEW
116.0 7.0 5.0 19 KB

WS-Machine is a websocket finite state machine for client websocket connections (Go)

License: MIT License

Go 100.00%
websocket go golang fsm state-machine networking async asynchronous non-blocking select

ws-machine's Introduction

WS-Machine

WS-Machine is a finite state machine for client websocket connections for Go. A caller just needs to provide a websocket URL and after that the state machine takes care of the connection. I.e. it connects to the server, reads/writes websocket messages, keeps the connection alive with pings, reconnects when the connection closes, waits when a connection attempt fails, etc.

Moreover it is fully asynchronous, the caller communicates with a machine via 4 Go channels:

  • Input is used to receive []byte messages from a websocket server;
  • Output is for sending []byte messages to a websocket server;
  • Status lets the user know when the machine state changes;
  • Command allows the user to control the state machine.

Every machine has 4 states:

  • CONNECTING is when it attempts to connect to a remote server;
  • CONNECTED means the connection has been established;
  • DISCONNECTED should be obvious;
  • WAITING triggers after a failed attempt to connect when the machine makes a pause before the next retry.

Because everything is done via Go channels it is now possible to integrate multiple websockets with timers, other channes and network connections in a single select loop. Thus avoiding possible dead-locks, complex logic and making the code simple, readable, clear and easy to modify/support. As they say Make websockets great again!

Under the hood the library uses gorilla/websocket to handle raw websocket connections.

In order to shutdown a running FSM a user should either close the Command channel or send the machine.QUIT command.

By default new FSM use the binary message format to communicate with a server. Some websocket server implementations do not support those. In such cases one might switch the machine to the text message format by sending the machine.USE_TEXT command.

Example

// A stateful client for ws://echo.websocket.org
package main

import (
    "fmt"
    "net/http"
    "github.com/aglyzov/ws-machine"
)

func main() {
	wsm := machine.New("ws://echo.websocket.org", http.Header{})
	fmt.Println("URL:  ", wsm.URL)

	loop:
	for {
		select {
		case st, _ := <-wsm.Status:
			fmt.Println("STATE:", st.State)
			if st.Error != nil {
				fmt.Println(st.Error)
			}
			switch st.State {
			case machine.CONNECTED:
				msg := "test message"
				wsm.Output <- []byte(msg)
				fmt.Println("SENT: ", msg)
			case machine.DISCONNECTED:
				break loop
			}
		case msg, ok := <-wsm.Input:
			if ok {
				fmt.Println("RECV: ", string(msg))
				wsm.Command <- machine.QUIT
			}
		}
	}
}

See more examples.

ws-machine's People

Contributors

aglyzov avatar bitdeli-chef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ws-machine's Issues

hijacked connection

Getting error on the server side.
Using httprouter.
It works correctly with my other client implementation.
Using ws-machine, I got

http: response.WriteHeader on hijacked connection
http: response.Write on hijacked connection

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.