https://github.com/banyango/socker
Cleanly handle state of the client/server connection when using binary websocket messaging.
https://github.com/banyango/socker
connection golang state-machine webrtc websocket
Last synced: about 2 months ago
JSON representation
Cleanly handle state of the client/server connection when using binary websocket messaging.
- Host: GitHub
- URL: https://github.com/banyango/socker
- Owner: Banyango
- Created: 2019-05-10T22:13:39.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-21T17:40:39.000Z (about 7 years ago)
- Last Synced: 2023-03-24T03:03:54.107Z (over 3 years ago)
- Topics: connection, golang, state-machine, webrtc, websocket
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README

# Socker
A lib for doing client/server state logic using websockets.
My game server is using WebRTC DataChannels for handling the data sent from the clients to the servers. This is a twist on WebRTC where a client opens a connection with the server and each sets up a single data channel peer connection through a websocket. This gets us a reliable UDP connection to the browser, which is great because websockets run over TCP and if we use them for game data we'd potentially run into issues. However WebRTC with a websocket setup requires a few back and forth steps based on the state of each side.
`Socker helps facilitate moving a server/client state forwards based on handlers.`
_Anyways this project is probably pretty specific to my needs but I thought I'd throw it up on github because why not._
#### My Example
___
|Server|Client|
|------|------|
|Setup websocket| |
| | Client connects to websocket
|Server sends list of buffered scene creation data| |
| |Client creates scene and data from server|
| | Client sends handshake to notify that it's ready to receive webrtc connection info.
|Server gets handshake and sends webrtc data | |
| | Client sets up webrtc connection and sends back webrtc info|
| Server sets remoteDescription from the client| |
| **Server listens for further websocket data** | **Client sends websocket data** |
___
#### Server.go
```
// Client connects and sends a message
connection.Add(func(message []byte) bool {
// send all buffered data (other players, networked objects, state, etc)
return true
})
// Client sent the handshake for webrtc
connection.Add(func(message []byte) bool {
// Create the web rtc data channel
// Create Offer
// Send Offer to client.
return true
})
// Client sent the answer
connection.Add(func(message []byte) bool {
// Set RemoteDescription to answer.
// data channel is created.
return true
})
connection.Add(func(message []byte) bool {
// Done setting up connection
// Can use this for any client data that needs to make sure it gets to server. (Disconnect, Some kind of state)
return false
})
```
___
#### Client.go
```
// Server sends buffered data
connection.Add(func(message []byte) bool {
// Create all entities.
// Send webrtc connection request.
return true
})
// Server sends offer
connection.Add(func(message []byte) bool {
// set remotedescription to offer.
// create answer.
// send answer.
return true
})
connection.Add(func(message []byte) bool {
// handle server data messages that must reach client. (A player disconnects, joins, etc)
return false
})
```