Skip to content

A slim implementation of a websocket server using Swift and Vapor 4.0.

Notifications You must be signed in to change notification settings

Hupka/swift-websocket-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift Websocket Server Example using Vapor 4.0

This project includes a minimum working example for a websocket server written in Swift. To interact with it I recommend using websocat (Github-Link).

Part of the challenge was to binary-encode and decode JSON payload into predefined Swift structs which are stored in the Sources/App/websocket/messages folder.

How to use

  1. Build and run using swift build and swift run
  2. In this example the websocket is served under the /channel path. In the terminal connect to the websocket server using websocat:
    # '-b' transfers payload binary encoded
    websocat -b ws://127.0.0.1:8080/channel
  3. Paste into the console the JSON payload in the valid format (it is of type WebsocketMessage<Connect>):
    {"client":"C13C2DA8-13FA-4BA6-A361-61488AC5B66A","data":{"connect":true}}
  4. The websocket server should return the following message which should be displayed in the terminal decoded as string:
    {"client":"C13C2DA8-13FA-4BA6-A361-61488AC5B66A","data":{"name":"Adrian","male":true,"age":33}}

About

A slim implementation of a websocket server using Swift and Vapor 4.0.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages