Skip to content

LCL WebSocket is a cross-platform WebSocket library written in Swift and for Swift, built on top of SwiftNIO.

License

Notifications You must be signed in to change notification settings

Local-Connectivity-Lab/lcl-websocket

Repository files navigation

LCL WebSocket

LCL WebSocket is a cross-platform WebSocket [RFC 6455] library written in Swift and for Swift, built on top of SwiftNIO.

Features

  • Conform to all AutoBahn base cases
  • High-performance WebSocket client and server, on top of SwiftNIO
  • TLS/SSL support
  • Thread-safe
  • Cross-platform
  • Customizable configurations
  • Comprehensive error handling

Requirements

  • Swift 5.9+
  • macOS 10.15+, iOS 13+, Linux

Getting Started

Swift Package Manager

Add the following to your Package.swift:

dependencies: [
    .package(url: "https://github.com/Local-Connectivity-Lab/lcl-websocket.git", from: "1.0.0")
]

Basic Usage

Client

let config = LCLWebSocket.Configuration(
    maxFrameSize: 1 << 16,
    autoPingConfiguration: .enabled(pingInterval: .seconds(4), pingTimeout: .seconds(10)),
    leftoverBytesStrategy: .forwardBytes
)

// Initialize the client
var client = LCLWebSocket.client()
client.onOpen { websocket in
    websocket.send(.init(string: "hello"), opcode: .text, promise: nil)
}

client.onBinary { websocket, binary in
    print("received binary: \(binary)")
}

client.onText { websocket, text in
    print("received text: \(text)")
}

try client.connect(to: "ws://127.0.0.1:8080", configuration: config).wait()

Server

let config = LCLWebSocket.Configuration(
    maxFrameSize: 1 << 16,
    autoPingConfiguration: .disabled,
    leftoverBytesStrategy: .forwardBytes
)

server.onPing { websocket, buffer in
    print("onPing: \(buffer)")
    websocket.pong(data: buffer)
}
server.onPong { ws, buffer in
    print("onPong: \(buffer)")
}
server.onBinary { websocket, buffer in
    websocket.send(buffer, opcode: .binary, promise: nil)
}
server.onText { websocket, text in
    print("received text: \(text)")
    websocket.send(.init(string: text), opcode: .text, promise: nil)
}
server.onClosing { code, reason in
    print("on closing: \(String(describing: code)), \(String(describing: reason))")
}

// wait forever
try server.listen(host: "127.0.0.1", port: 8080, configuration: config).wait()

TODO

  • Support Swift Concurrency
  • Support WebSocket Compression Extension

Contributing

Any contribution and pull requests are welcome! However, before you plan to implement some features or try to fix an uncertain issue, it is recommended to open a discussion first. You can also join our Discord channel, or visit our website.

License

LCLPing is released under Apache License. See LICENSE for more details.

About

LCL WebSocket is a cross-platform WebSocket library written in Swift and for Swift, built on top of SwiftNIO.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published