forked from vibe-d/vibe.d
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vibe-d#1215 from dariusc93/master
Websocket example using the WebInterface
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "web-websockets-example", | ||
"description": "Example for using the WebSocket feature with WebInterface", | ||
"authors": [ "Darius C." ], | ||
"dependencies": { | ||
"vibe-d": {"version": "~master", "path": "../../"} | ||
}, | ||
"versions": ["VibeDefaultMain"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var socket | ||
|
||
function connect() | ||
{ | ||
setText("connecting..."); | ||
socket = new WebSocket(getBaseURL() + "/ws"); | ||
socket.onopen = function() { | ||
setText("connected. waiting for timer..."); | ||
} | ||
socket.onmessage = function(message) { | ||
setText(message.data); | ||
} | ||
socket.onclose = function() { | ||
setText("connection closed."); | ||
} | ||
socket.onerror = function() { | ||
setText("Error!"); | ||
} | ||
} | ||
|
||
function closeConnection() | ||
{ | ||
socket.close(); | ||
setText("closed."); | ||
} | ||
|
||
function setText(text) | ||
{ | ||
document.getElementById("timer").innerHTML = text; | ||
} | ||
|
||
function getBaseURL() | ||
{ | ||
var href = window.location.href.substring(7); // strip "http://" | ||
var idx = href.indexOf("/"); | ||
return "ws://" + href.substring(0, idx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module app; | ||
|
||
import vibe.core.core : sleep; | ||
|
||
import vibe.core.log; | ||
import vibe.http.fileserver : serveStaticFiles; | ||
import vibe.http.router : URLRouter; | ||
import vibe.http.server; | ||
import vibe.web.web; | ||
import vibe.http.websockets : WebSocket, handleWebSockets; | ||
|
||
import core.time; | ||
import std.conv : to; | ||
|
||
class WebsocketService { | ||
|
||
@path("/") void getHome() | ||
{ | ||
render!("index.dt"); | ||
} | ||
|
||
@path("/ws") void getWebsocket(WebSocket socket){ | ||
int counter = 0; | ||
logInfo("Got new web socket connection."); | ||
while (true) { | ||
sleep(1.seconds); | ||
if (!socket.connected) break; | ||
counter++; | ||
logInfo("Sending '%s'.", counter); | ||
socket.send(counter.to!string); | ||
} | ||
logInfo("Client disconnected."); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
shared static this() | ||
{ | ||
auto router = new URLRouter; | ||
|
||
router.registerWebInterface(new WebsocketService); | ||
|
||
router.get("*", serveStaticFiles("public/")); | ||
|
||
auto settings = new HTTPServerSettings; | ||
settings.port = 8080; | ||
settings.bindAddresses = ["::1", "127.0.0.1"]; | ||
listenHTTP(settings, router); | ||
|
||
logInfo("Please open http://127.0.0.1:8080/ in your browser."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
doctype html | ||
html | ||
head | ||
title WebSocket Test | ||
script(type='text/javascript', src='/scripts/websocket.js') | ||
style. | ||
pre#timer { | ||
border: 1px solid black; | ||
} | ||
body(onload='connect()') | ||
p The following box should show a running counter, updated by the server: | ||
pre#timer. | ||
button(type='button', onclick='closeConnection()') Close connection |