/WebSocket

WebSockets server for Swift 2.2 on Linux

Primary LanguageSwiftMIT LicenseMIT

WebSocket

Swift 2.2 Platforms Linux License MIT Slack Status

WebSocket server for Swift 2.2.

Dependencies

WebSocket is made of:

  • HTTP - HTTP request/response
  • Venice - CSP and TCP/IP

Related Projects

  • Epoch - Venice based HTTP server

Usage

WebSocket + Epoch

You'll need an HTTP server. WebSocket and Epoch were designed to work with each other seamlessly.

#if os(Linux)
    import Glibc
#else
    import Darwin.C
#endif
import HTTP
import Epoch
import CHTTPParser
import CLibvenice
import WebSocket

let webSocketServer = WebSocketServer { webSocket in
	print("WebSocket connected")
	webSocket.listen { event in
		switch event {
		case .Binary(let data):
			webSocket.send(data)
		case .Text(let text):
			webSocket.send(text)
		case .Ping(let data):
			webSocket.pong(data)
		case .Pong(let data):
			break
		case .Close(let code, let reason):
			print("WebSocket closed")
		}
	}
}

let server = Server(port: 8080, responder: webSocketServer)
server.start()

Installation

WebSocket depends on the C lib libvenice. Install it through:

Homebrew

$ brew tap zewo/tap
$ brew install libvenice

Ubuntu/Debian

$ add-apt-repository 'deb [trusted=yes] http://apt.zewo.io/deb ./'
$ apt-get install libvenice

Source

$ git clone https://github.com/Zewo/libvenice.git && cd libvenice
$ make
$ (sudo) make install

You only have to install the C libs once.

Then add WebSocket to your Package.swift

import PackageDescription

let package = Package(
	dependencies: [
		.Package(url: "https://github.com/Zewo/WebSocket.git", majorVersion: 0, minor: 1)
	]
)

Community

Slack

Join us on Slack.

License

WebSocket is released under the MIT license. See LICENSE for details.