All Projects → aglyzov → Ws Machine

aglyzov / Ws Machine

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

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Ws Machine

Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+191.82%)
Mutual labels:  asynchronous, non-blocking, websocket
Base64 Async
Non-blocking chunked Base64 encoding
Stars: ✭ 98 (-10.91%)
Mutual labels:  async, asynchronous, non-blocking
Mio
Metal IO library for Rust
Stars: ✭ 4,613 (+4093.64%)
Mutual labels:  asynchronous, non-blocking, networking
Microwebsrv2
The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
Stars: ✭ 295 (+168.18%)
Mutual labels:  async, asynchronous, websocket
Parallel Ssh
Asynchronous parallel SSH client library.
Stars: ✭ 864 (+685.45%)
Mutual labels:  async, asynchronous, non-blocking
Ea Async
EA Async implements async-await methods in the JVM.
Stars: ✭ 1,085 (+886.36%)
Mutual labels:  async, asynchronous
Ktor
Framework for quickly creating connected applications in Kotlin with minimal effort
Stars: ✭ 9,190 (+8254.55%)
Mutual labels:  async, asynchronous
Peony Twitter
An asynchronous Twitter API client for Python 3.5+
Stars: ✭ 62 (-43.64%)
Mutual labels:  async, asynchronous
Radon
Object oriented state management solution for front-end development.
Stars: ✭ 80 (-27.27%)
Mutual labels:  async, asynchronous
Uvloop
Ultra fast asyncio event loop.
Stars: ✭ 8,246 (+7396.36%)
Mutual labels:  async, networking
Framework
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.
Stars: ✭ 1,125 (+922.73%)
Mutual labels:  async, asynchronous
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (-23.64%)
Mutual labels:  state-machine, fsm
Channelstream
Channelstream is a websocket communication server for web applications
Stars: ✭ 52 (-52.73%)
Mutual labels:  async, websocket
Before After Hook
wrap methods with before/after hooks
Stars: ✭ 49 (-55.45%)
Mutual labels:  async, asynchronous
Avenue
Wrapper around URLSession and URLSessionTask to enable seamless integration with Operation / OperationQueue.
Stars: ✭ 58 (-47.27%)
Mutual labels:  async, networking
Vibe.d
Official vibe.d development
Stars: ✭ 1,043 (+848.18%)
Mutual labels:  async, networking
Tanya
GC-free, high-performance D library: Containers, networking, metaprogramming, memory management, utilities
Stars: ✭ 70 (-36.36%)
Mutual labels:  async, networking
Workerman
An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols. PHP>=5.3.
Stars: ✭ 9,617 (+8642.73%)
Mutual labels:  asynchronous, websocket
Datastore
🐹 Bloat free and flexible interface for data store and database access.
Stars: ✭ 99 (-10%)
Mutual labels:  async, asynchronous
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+1310%)
Mutual labels:  asynchronous, websocket

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].