Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created August 22, 2014 16:33
Show Gist options
  • Save kyubuns/1c91465f590a521b738b to your computer and use it in GitHub Desktop.
Save kyubuns/1c91465f590a521b738b to your computer and use it in GitHub Desktop.
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.http.websockets : WebSocket, handleWebSockets;
import core.time;
shared static this()
{
auto router = new URLRouter;
router.get("/ws", handleWebSockets(&handleWebSocketConnection));
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["127.0.0.1"];
listenHTTP(settings, router);
}
int hoge(int a, int b)
{
return a+b;
}
unittest
{
assert(hoge(1,2) == 5);
}
void handleWebSocketConnection(scope WebSocket socket)
{
static int hoge = 0;
static WebSocket[] socketList;
socketList ~= socket;
int id = hoge++;
logInfo("open %s", id);
while (true)
{
socket.waitForData();
if (!socket.connected) break;
auto text = socket.receiveText;
logInfo("%s: %s", id, text);
foreach (WebSocket s; socketList) s.send(text);
}
logInfo("close %s", id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment