Permalink
Browse files

Added example chat program.

git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@836 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information...
1 parent 92d0daf commit cc58e730613d0c33b80ed4d16dea0cf2fcae048a Johan Bevemyr committed Feb 16, 2005
View
@@ -0,0 +1,18 @@
+SUBDIRS = src
+include ./include.mk
+
+
+all debug clean install:
+ @set -e ; \
+ for d in $(SUBDIRS) ; do \
+ if [ -f $$d/Makefile ]; then ( cd $$d && $(MAKE) $@ ) || exit 1 ; fi ; \
+ done
+
+
+touch:
+ find . -name '*' -print | xargs touch -m
+ find . -name '*.erl' -print | xargs touch -m
+
+
+
+
@@ -0,0 +1,119 @@
+// -*- Fundamental -*-
+// File: chat.js (/var/www/chat.js)
+// Author: Johan Bevemyr
+// Created: Thu Nov 18 18:25:53 2004
+// Purpose:
+
+
+/*
+ // Uncomment this to get debug printouts in a separate window.
+
+var _console=null;
+var _console_txt = null;
+var _cnt = 0;
+
+function debug(msg)
+{
+ var re = new RegExp("<","gi");
+ msg = msg.replace(re, "&lt;")
+
+ if ((_console==null) || (_console.closed)) {
+ _console = window.open("","console","width=600,height=300,resizable=yes,scrollbars=yes");
+ _console.document.open("text/plain");
+ _console.document.writeln("<pre>");
+ }
+ _cnt++;
+ _console.document.write(_cnt + ": ");
+ _console.document.writeln(msg);
+}
+
+*/
+
+function debug(msg) {
+}
+
+function sendmsg() {
+ var message = document.getElementById("msg").value;
+
+ debug("sendmsg: sending "+message);
+ xml_sender.abort();
+ xml_sender.open("POST", "chat_write.yaws", false);
+ xml_sender.send(message);
+ debug("sendmsg: sent "+message);
+ document.getElementById("msg").value = "";
+}
+
+function create_xmlhttp() {
+ var xmlhttp;
+
+ try {
+ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
+ } catch (e) {
+ try {
+ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
+ } catch (E) {
+ xmlhttp = false;
+ }
+ }
+
+ if (!xmlhttp && typeof XMLHttpRequest!='undefined')
+ xmlhttp = new XMLHttpRequest();
+
+ return xmlhttp;
+}
+
+var xml_sender = create_xmlhttp();
+var xml_reader = create_xmlhttp();
+
+function reader_init() {
+ var h = function() {
+ debug("reader_init(): got " + xml_reader.readyState);
+
+ if (xml_reader.readyState == 4) {
+ var msgs = document.getElementById("msgs");
+ var reply = xml_reader.responseText;
+
+ debug("reader_init(): something");
+
+ if (reply.substring(0,2) == "ok") {
+ msgs.value = msgs.value + "\n" + reply.substring(2);
+ move_to_end(msgs);
+ setTimeout("reader_init()", 0);
+ }
+ else if (reply.substring(0,7) == "timeout") {
+ setTimeout("reader_init()", 0);
+ }
+ else {
+ alert("Chat server got unsupported reply.'" + reply+"'");
+ }
+ }
+ }
+
+ debug("reader_init(): waiting");
+
+ xml_reader.open("GET", "chat_read.yaws", true);
+
+ if (window.XMLHttpRequest) {
+ xml_reader.onload=h;
+ }
+ else {
+ xml_reader.onreadystatechange=h;
+ }
+
+ xml_reader.send(null);
+ debug("reader_init: sent read request");
+}
+
+function move_to_end(element) {
+ try {
+ element.scrollTop = element.scrollHeight;
+ } catch(E) {
+ }
+}
+
+function stop_all() {
+ try {
+ xml_reader.abort();
+ } catch (E) {
+ }
+}
@@ -0,0 +1,23 @@
+<erl>
+out(A) ->
+ case chat:check_session(A) of
+ {ok, Session} ->
+ chat:dynamic_headers();
+ Error ->
+ Error++[break]
+ end.
+</erl>
+
+<html>
+
+<script src="chat.js"></script>
+
+<body onload="setTimeout('reader_init()', 0);" onunload="stop_all()">
+<H1> Welcome to the chat room </h1>
+<form action="javascript:sendmsg()">
+<textarea id="msgs" name="msgs" cols=80 rows=25>
+</textarea><br>
+<input id="msg" name="msg" type="text" size="80" value=""
+style='font-family:courier'>
+</form>
+</html>
@@ -0,0 +1,4 @@
+<erl>
+out(A) ->
+ chat:chat_read(A).
+</erl>
@@ -0,0 +1,10 @@
+<erl>
+out(A) ->
+ case chat:check_session(A) of
+ Session ->
+ chat:chat_write(A);
+ Error ->
+ Error
+ end.
+
+</erl>
@@ -0,0 +1,4 @@
+<erl>
+out(A) ->
+ {redirect_local, {rel_path, "chat.yaws"}}.
+</erl>
@@ -0,0 +1,24 @@
+<erl>
+
+out(A) ->
+ L = case (A#arg.req)#http_request.method of
+ 'GET' ->
+ yaws_api:parse_query(A);
+ 'POST' ->
+ yaws_api:parse_post(A)
+ end,
+ case {lists:keysearch("user", 1, L),
+ lists:keysearch("password", 1, L)} of
+ {{value, {_, User}}, {value, {_,Passwd}}} ->
+ case chat:login(User, Passwd) of
+ {ok, Cookie} ->
+ [yaws_api:setcookie("sessionid", Cookie),
+ {redirect_local, {rel_path,"chat.yaws"}}];
+ {error, Reason} ->
+ chat:display_login(A, Reason)
+ end;
+ _ ->
+ chat:display_login(A, "badly formated POST/GET")
+ end.
+
+</erl>
@@ -0,0 +1,53 @@
+###-*-makefile-*- ; force emacs to enter makefile-mode
+
+
+# these the items can be edited
+
+INSTALLPREFIX=/var/yaws
+
+CHATINSTALLDIR=$(INSTALLPREFIX)/chat
+EBININSTALLDIR=$(INSTALLPREFIX)/ebin
+
+ERL=$(shell which erl)
+ERLC=$(shell which erlc)
+
+
+## don't edit below here
+
+EMULATOR=beam
+ifdef debug
+ ERLC_FLAGS+=-Ddebug
+endif
+
+ifdef trace
+ ERLC_FLAGS=+trace
+endif
+
+ifdef export_all
+ ERLC_FLAGS+=-Dexport_all
+endif
+
+
+INSTALL=install -c
+INSTALL_DATA=${INSTALL} -m 644
+
+
+# Hmm, don't know if you are supposed to like this better... ;-)
+APPSCRIPT = '$$vsn=shift; $$mods=""; while(@ARGV){ $$_=shift; s/^([A-Z].*)$$/\'\''$$1\'\''/; $$mods.=", " if $$mods; $$mods .= $$_; } while(<>) { s/%VSN%/$$vsn/; s/%MODULES%/$$mods/; print; }'
+
+# Targets
+
+../ebin/%.app: %.app.src ../vsn.mk Makefile
+ perl -e $(APPSCRIPT) "$(VSN)" $(MODULES) < $< > $@
+
+../ebin/%.appup: %.appup
+ cp $< $@
+
+../ebin/%.$(EMULATOR): %.erl
+ $(ERLC) -b $(EMULATOR) $(ERLC_FLAGS) -o ../ebin $<
+
+%.$(EMULATOR): %.erl
+ $(ERLC) -b $(EMULATOR) $(ERLC_FLAGS) $<
+
+
+
@@ -0,0 +1,46 @@
+include ../include.mk
+
+ifeq ($(TYPE),debug)
+DEBUG_FLAGS = -Ddebug
+else
+DEBUG_FLAGS =
+endif
+
+#include ../vsn.mk
+#VSN=$(YAWS_VSN)
+MODULES=chat
+
+HDR_FILES=
+
+EBIN_FILES=$(MODULES:%=../ebin/%.$(EMULATOR))
+
+ERLC_FLAGS+=-W +debug_info $(DEBUG_FLAGS) -I../../../include -pa ../../chat
+
+#
+# Targets
+#
+
+all: $(EBIN_FILES)
+
+$(EBIN_FILES) : $(HDR_FILES)
+
+debug:
+ $(MAKE) TYPE=debug
+
+clean:
+ rm -f $(EBIN_FILES)
+
+install: all
+ install -d $(CHATINSTALLDIR)
+ install -d $(EBININSTALLDIR)
+ (cd ../ebin; find . -name '*' -print | xargs tar cf - ) | (cd $(EBININSTALLDIR); tar xf - )
+ (cd ../chat; find . -name '*' -print | xargs tar cf - ) | (cd $(CHATINSTALLDIR); tar xf - )
+
+
+
+tags:
+ erl -noshell -pa ../ebin -s chat_debug mktags
+
+xref:
+ erl -noshell -pa ../ebin -s chat_debug xref ../ebin
+
Oops, something went wrong.

0 comments on commit cc58e73

Please sign in to comment.