From e1cdf119504fc33848e043f23b752b455f369609 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 3 Nov 2016 13:44:09 -0400 Subject: [PATCH 1/5] websockets docs --- couscous.yml | 7 ++++++ websockets/example.md | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 websockets/example.md diff --git a/couscous.yml b/couscous.yml index 9f2680b2..f60e0a55 100644 --- a/couscous.yml +++ b/couscous.yml @@ -140,3 +140,10 @@ menu: http-server: text: Server relativeUrl: http/server.html + + web-sockets: + name: WebSockets + items: + websockets-example: + text: Example + relativeUrl: websockets/example.html diff --git a/websockets/example.md b/websockets/example.md new file mode 100644 index 00000000..91e506ed --- /dev/null +++ b/websockets/example.md @@ -0,0 +1,56 @@ +--- +currentMenu: websockets-example +--- + +# Using WebSockets + +Below are some examples of WebSockets in use. + +## Client + +```Swift +import WebSockets + +try WebSocket.connect(to: url) { ws in + print("Connected to \(url)") + + ws.onText = { ws, text in + print("[event] - \(text)") + } + + ws.onClose = { ws, _, _, _ in + print("\n[CLOSED]\n") + } +} +``` + +## Server + +```Swift +import HTTP +import WebSockets + +final class MyResponder: Responder { + func respond(to request: Request) throws -> Response { + return try request.upgradeToWebSocket { ws in + print("[ws connected]") + + ws.onText = { ws, text in + print("[ws text] \(text)") + try ws.send("🎙 \(text)") + } + + ws.onClose = { _, code, reason, clean in + print("[ws close] \(clean ? "clean" : "dirty") \(code?.description ?? "") \(reason ?? "")") + } + } + } +} + +let server = try Server, Serializer>(port: port) + +print("Connect websocket to http://localhost:\(port)/") +try server.start(responder: MyResponder()) { error in + print("Got server error: \(error)") +} +``` From 35f7205829cd1b4cd1a4894ac27c72e5cad82a9a Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 3 Nov 2016 13:51:33 -0400 Subject: [PATCH 2/5] add port --- websockets/example.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/websockets/example.md b/websockets/example.md index 91e506ed..77dd8cd6 100644 --- a/websockets/example.md +++ b/websockets/example.md @@ -29,6 +29,7 @@ try WebSocket.connect(to: url) { ws in ```Swift import HTTP import WebSockets +import Transport final class MyResponder: Responder { func respond(to request: Request) throws -> Response { @@ -47,6 +48,7 @@ final class MyResponder: Responder { } } +let port = 8080 let server = try Server, Serializer>(port: port) print("Connect websocket to http://localhost:\(port)/") From 4e70890a19cb65ce14ff5d70b1496982aba251ca Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 3 Nov 2016 14:20:10 -0400 Subject: [PATCH 3/5] web socket update --- websockets/example.md | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/websockets/example.md b/websockets/example.md index 77dd8cd6..0ebf0780 100644 --- a/websockets/example.md +++ b/websockets/example.md @@ -6,6 +6,61 @@ currentMenu: websockets-example Below are some examples of WebSockets in use. +## Droplet + +Creating a WebSocket server with the Droplet is easy. WebSockets work by upgrading an HTTP request to a WebSocket connection. + +Because of this, you should pick a URL for your WebSocket server to reside at. In this case, we use `/ws`. + +```swift +import Vapor + +let drop = Droplet() + +drop.socket("ws") { req, ws in + print("New WebSocket connected: \(ws)") + + // ping the socket to keep it open + try background { + if ws.state == .open { + try? ws.ping() + drop.console.wait(seconds: 10) // every 10 seconds + } + } + + ws.onText = { ws, text in + print("Text received: \(text)") + + // reverse the characters and send back + let rev = String(text.characters.reversed()) + try ws.send(rev.bytes) + } + + ws.onClose = { ws, code, reason, clean in + print("Closed.") + } +} + +drop.run() +``` + +To connect with a WebSocket client, you would open a connection to `ws:///ws`. + +Here is an example using JavaScript. + +```js + +var ws = new WebSocket("ws://0.0.0.0:8080/ws") + +ws.onmessage = function(msg) { + console.log(msg) +} + +ws.send('test') +``` + +The above will log `tset` (`test` reversed). + ## Client ```Swift From 2d5b028b7ee1bc8b18e0bdbdf8e583c91311066c Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 3 Nov 2016 14:24:30 -0400 Subject: [PATCH 4/5] socket updates --- couscous.yml | 9 ++-- websockets/example.md | 113 ------------------------------------------ 2 files changed, 6 insertions(+), 116 deletions(-) delete mode 100644 websockets/example.md diff --git a/couscous.yml b/couscous.yml index f60e0a55..efb85cdc 100644 --- a/couscous.yml +++ b/couscous.yml @@ -144,6 +144,9 @@ menu: web-sockets: name: WebSockets items: - websockets-example: - text: Example - relativeUrl: websockets/example.html + websockets-droplet: + text: Droplet + relativeUrl: websockets/droplet.html + websockets-custom: + text: Custom + relativeUrl: websockets/custom.html diff --git a/websockets/example.md b/websockets/example.md deleted file mode 100644 index 0ebf0780..00000000 --- a/websockets/example.md +++ /dev/null @@ -1,113 +0,0 @@ ---- -currentMenu: websockets-example ---- - -# Using WebSockets - -Below are some examples of WebSockets in use. - -## Droplet - -Creating a WebSocket server with the Droplet is easy. WebSockets work by upgrading an HTTP request to a WebSocket connection. - -Because of this, you should pick a URL for your WebSocket server to reside at. In this case, we use `/ws`. - -```swift -import Vapor - -let drop = Droplet() - -drop.socket("ws") { req, ws in - print("New WebSocket connected: \(ws)") - - // ping the socket to keep it open - try background { - if ws.state == .open { - try? ws.ping() - drop.console.wait(seconds: 10) // every 10 seconds - } - } - - ws.onText = { ws, text in - print("Text received: \(text)") - - // reverse the characters and send back - let rev = String(text.characters.reversed()) - try ws.send(rev.bytes) - } - - ws.onClose = { ws, code, reason, clean in - print("Closed.") - } -} - -drop.run() -``` - -To connect with a WebSocket client, you would open a connection to `ws:///ws`. - -Here is an example using JavaScript. - -```js - -var ws = new WebSocket("ws://0.0.0.0:8080/ws") - -ws.onmessage = function(msg) { - console.log(msg) -} - -ws.send('test') -``` - -The above will log `tset` (`test` reversed). - -## Client - -```Swift -import WebSockets - -try WebSocket.connect(to: url) { ws in - print("Connected to \(url)") - - ws.onText = { ws, text in - print("[event] - \(text)") - } - - ws.onClose = { ws, _, _, _ in - print("\n[CLOSED]\n") - } -} -``` - -## Server - -```Swift -import HTTP -import WebSockets -import Transport - -final class MyResponder: Responder { - func respond(to request: Request) throws -> Response { - return try request.upgradeToWebSocket { ws in - print("[ws connected]") - - ws.onText = { ws, text in - print("[ws text] \(text)") - try ws.send("🎙 \(text)") - } - - ws.onClose = { _, code, reason, clean in - print("[ws close] \(clean ? "clean" : "dirty") \(code?.description ?? "") \(reason ?? "")") - } - } - } -} - -let port = 8080 -let server = try Server, Serializer>(port: port) - -print("Connect websocket to http://localhost:\(port)/") -try server.start(responder: MyResponder()) { error in - print("Got server error: \(error)") -} -``` From 81fbf36d5301d309fc333d1da7ca6677d6a2faed Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Thu, 3 Nov 2016 14:24:37 -0400 Subject: [PATCH 5/5] socket droplet --- websockets/custom.md | 58 +++++++++++++++++++++++++++++++++++++++++++ websockets/droplet.md | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 websockets/custom.md create mode 100644 websockets/droplet.md diff --git a/websockets/custom.md b/websockets/custom.md new file mode 100644 index 00000000..df323e6d --- /dev/null +++ b/websockets/custom.md @@ -0,0 +1,58 @@ +--- +currentMenu: websockets-custom +--- + +# Custom WebSockets + +Below are some examples of WebSockets using the underlying Engine package. + +## Client + +```Swift +import WebSockets + +try WebSocket.connect(to: url) { ws in + print("Connected to \(url)") + + ws.onText = { ws, text in + print("[event] - \(text)") + } + + ws.onClose = { ws, _, _, _ in + print("\n[CLOSED]\n") + } +} +``` + +## Server + +```Swift +import HTTP +import WebSockets +import Transport + +final class MyResponder: Responder { + func respond(to request: Request) throws -> Response { + return try request.upgradeToWebSocket { ws in + print("[ws connected]") + + ws.onText = { ws, text in + print("[ws text] \(text)") + try ws.send("🎙 \(text)") + } + + ws.onClose = { _, code, reason, clean in + print("[ws close] \(clean ? "clean" : "dirty") \(code?.description ?? "") \(reason ?? "")") + } + } + } +} + +let port = 8080 +let server = try Server, Serializer>(port: port) + +print("Connect websocket to http://localhost:\(port)/") +try server.start(responder: MyResponder()) { error in + print("Got server error: \(error)") +} +``` diff --git a/websockets/droplet.md b/websockets/droplet.md new file mode 100644 index 00000000..bc103fe1 --- /dev/null +++ b/websockets/droplet.md @@ -0,0 +1,57 @@ +--- +currentMenu: websockets-droplet +--- + +# Droplet WebSockets + +Creating a WebSocket server with the Droplet is easy. WebSockets work by upgrading an HTTP request to a WebSocket connection. + +Because of this, you should pick a URL for your WebSocket server to reside at. In this case, we use `/ws`. + +```swift +import Vapor + +let drop = Droplet() + +drop.socket("ws") { req, ws in + print("New WebSocket connected: \(ws)") + + // ping the socket to keep it open + try background { + if ws.state == .open { + try? ws.ping() + drop.console.wait(seconds: 10) // every 10 seconds + } + } + + ws.onText = { ws, text in + print("Text received: \(text)") + + // reverse the characters and send back + let rev = String(text.characters.reversed()) + try ws.send(rev.bytes) + } + + ws.onClose = { ws, code, reason, clean in + print("Closed.") + } +} + +drop.run() +``` + +To connect with a WebSocket client, you would open a connection to `ws:///ws`. + +Here is an example using JavaScript. + +```swift +var ws = new WebSocket("ws://0.0.0.0:8080/ws") + +ws.onmessage = function(msg) { + console.log(msg) +} + +ws.send("test") +``` + +The above will log `tset` (`test` reversed). \ No newline at end of file