Skip to content

Commit

Permalink
adds websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
kirel committed Jul 15, 2010
1 parent ab5a770 commit 5bcda58
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Server.hs → Webserver.hs
@@ -1,4 +1,4 @@
module Server where
module Main where

import Network.Loli
import Network.Loli.Utils
Expand Down Expand Up @@ -53,7 +53,8 @@ jsonResults = encode . (Prelude.map toJSO) . sortBySnd where -- FIXME sorting co
-- server
main = do
c <- classifier
(run 3000) . loli $ do
print "Server starting at port 3000"
(run 3000) . loli $ do

get "/env" $ do
env <- ask
Expand Down
Binary file added Websockets
Binary file not shown.
24 changes: 24 additions & 0 deletions Websockets.hs
@@ -0,0 +1,24 @@
import Network
import qualified Network.Websocket as WS

config = WS.Config {
WS.configPort = 9876,
WS.configOrigins = Nothing,
WS.configDomains = Nothing,
WS.configOnOpen = onOpen,
WS.configOnMessage = onMessage,
WS.configOnClose = onClose
}

main = withSocketsDo $ WS.startServer config


onOpen ws = do
putStrLn "Connection opened"

onMessage ws msg = do
putStrLn $ "Received message: " ++ msg
WS.send ws msg

onClose ws = do
putStrLn "Connection closed"
48 changes: 48 additions & 0 deletions Websockets.html
@@ -0,0 +1,48 @@
<html>
<head>
<title>Web Sockets</title>

<script src="http://www.google.com/jsapi"></script>
<script>
google.load('jquery','1.3.2');
</script>

<script>
$(document).ready(function() {

if ("WebSocket" in window) {
var ws = new WebSocket("ws://localhost:9876/");
ws.onopen = function() {
$('#connectionStatus').text('Connection opened');

ws.send("Testing");
ws.send("Testing");
ws.send("1 2 3");
ws.send("It works!");
};

ws.onmessage = function(evt) {
$('#output').append('<p>' + evt.data + '</p>');
};

ws.onclose = function() {
$('#connectionStatus').text('Connection closed');
};

} else {
$('#connectionStatus').append('<p>Your browser does not support web sockets</p>');
}
});
</script>

</head>
<body>
<h1>Websockets example</h1>

<div id="output">
</div>

<div id="connectionStatus">
</div>

</body>

0 comments on commit 5bcda58

Please sign in to comment.