Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ferenc Vehmann
committed
Sep 19, 2011
0 parents
commit 0d94d27
Showing
5 changed files
with
188 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,4 @@ | ||
.settings/ | ||
.project/ | ||
.DS_Store | ||
ebin/* |
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,91 @@ | ||
%% Author: fatso | ||
%% Created: Aug 29, 2011 | ||
%% Description: TODO: Add description to hello | ||
-module(hello). | ||
|
||
%% | ||
%% Include files | ||
%% | ||
-include("/usr/local/lib/yaws/include/yaws_api.hrl"). | ||
|
||
%% | ||
%% Exported Functions | ||
%% | ||
-export([out/1]). | ||
|
||
%% | ||
%% API Functions | ||
%% | ||
|
||
out(A) -> | ||
io:format("connection: ~p~n", [A]), | ||
case get_upgrade_header(A#arg.headers) of | ||
undefined -> | ||
{content, "text/plain", "You're not a web sockets client! Go000 away!"}; | ||
"WebSocket" -> | ||
WebSocketOwner = spawn(fun() -> websocket_owner() end), | ||
{websocket, WebSocketOwner, passive} | ||
end. | ||
|
||
websocket_owner() -> | ||
io:format("websocket_owner: ~p~n", [self()]), | ||
receive | ||
{ok, WebSocket} -> | ||
%% This is how we read messages (plural!!) from websockets on passive mode | ||
case yaws_api:websocket_receive(WebSocket) of | ||
{error,closed} -> | ||
io:format("The websocket got disconnected right from the start. " | ||
"This wasn't supposed to happen!!~n"); | ||
{ok, Messages} -> | ||
case Messages of | ||
[<<"hekas">>] -> | ||
yaws_api:websocket_setopts(WebSocket, [{active, true}]), | ||
echo_server(WebSocket); | ||
Other -> | ||
io:format("websocket_owner got: ~p. Terminating~n", [Other]) | ||
end | ||
end; | ||
_ -> ok | ||
end. | ||
|
||
echo_server(WebSocket) -> | ||
receive | ||
|
||
|
||
{tcp, WebSocket, DataFrame} -> | ||
Data = yaws_api:websocket_unframe_data(DataFrame), | ||
io:format("Got data from Websocket: ~p~n", [Data]), | ||
yaws_api:websocket_send(WebSocket, Data), | ||
echo_server(WebSocket); | ||
|
||
{tcp_closed, WebSocket} -> | ||
io:format("Websocket closed. Terminating echo_server...~n"); | ||
|
||
Any -> | ||
io:format("echo_server received msg:~p~n", [Any]), | ||
echo_server(WebSocket) | ||
end. | ||
|
||
%% | ||
%% Local Functions | ||
%% | ||
|
||
get_upgrade_header(#headers{other=L}) -> | ||
lists:foldl(fun({http_header,_,K0,_,V}, undefined) -> | ||
K = case is_atom(K0) of | ||
true -> | ||
atom_to_list(K0); | ||
false -> | ||
K0 | ||
end, | ||
case string:to_lower(K) of | ||
"upgrade" -> | ||
V; | ||
_ -> | ||
undefined | ||
end; | ||
(_, Acc) -> | ||
Acc | ||
end, undefined, L). | ||
|
||
|
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,56 @@ | ||
<html> | ||
<body> | ||
|
||
<div id="div"></div> | ||
<script type="text/javascript"> | ||
|
||
var div = document.getElementById("div"); | ||
var socketOpen = false; | ||
|
||
if ("WebSocket" in window) { | ||
div.innerHTML += "connecting ... "; | ||
var ws = new WebSocket("ws://localhost:7000/"); | ||
ws.onopen = function() { | ||
// Web Socket is connected. You can send data by send() method. | ||
// ws.send("message to send"); | ||
socketOpen = true; | ||
div.innerHTML += "connected <br/>"; | ||
}; | ||
ws.onmessage = function (evt) { | ||
var received_msg = evt.data; | ||
div.innerHTML += evt.data + "<br/>"; | ||
}; | ||
ws.onclose = function() { | ||
// websocket is closed. | ||
div.innerHTML += "closed <br/>"; | ||
|
||
}; | ||
|
||
ws.onerror = function() { | ||
div.innerHTML += "error <br/>"; | ||
}; | ||
} else { | ||
div.innerHTML += "ws not supported <br/>"; | ||
} | ||
|
||
|
||
function mySubmit() { | ||
if (!socketOpen) {return;} | ||
|
||
var inp = document.getElementById("inp"); | ||
|
||
ws.send(inp.value) | ||
div.innerHTML += inp.value + "<br/>"; | ||
|
||
inp.value = ""; | ||
|
||
return false; | ||
} | ||
|
||
|
||
</script> | ||
<form id="xform" onsubmit="return mySubmit()"> | ||
<input id="inp" type="text" /> | ||
</form> | ||
</body> | ||
</html> |
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,19 @@ | ||
-module(ybed). | ||
-compile(export_all). | ||
-include("/usr/local/lib/yaws/include/yaws_api.hrl"). | ||
|
||
start() -> | ||
{ok, spawn(?MODULE, run, [])}. | ||
|
||
run() -> | ||
Id = "embedded", | ||
Docroot = "/tmp/yawstest", | ||
GconfList = [{id, Id}], | ||
SconfList = [{port, 7000}, | ||
{listen, {0,0,0,0}}, | ||
{docroot, Docroot}, | ||
{appmods, [{"/", hello}]}], | ||
{ok, SCList, GC, ChildSpecs} = yaws_api:embedded_start_conf(Docroot, SconfList, GconfList, Id), | ||
[supervisor:start_child(ybed_sup, Ch) || Ch <- ChildSpecs], | ||
yaws_api:setconf(GC, SCList), | ||
{ok, self()}. |
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,18 @@ | ||
-module(ybed_sup). | ||
-behaviour(supervisor). | ||
|
||
%% API | ||
-export([start_link/0]). | ||
|
||
%% Supervisor callbacks | ||
-export([init/1]). | ||
|
||
start_link() -> | ||
io:format("starting"), | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
init([]) -> | ||
YBed = {ybed, {ybed,start,[]}, | ||
permanent,2000,worker,[ybed]}, | ||
{ok,{{one_for_all,0,1}, [YBed]}}. | ||
|