Skip to content

Commit

Permalink
Start work on express usage for more elaborate chat server example
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial committed Oct 18, 2015
1 parent 6b1001e commit 6662f25
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
9 changes: 6 additions & 3 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pkgs := nodejs,socket_io
pkgs := nodejs,socket_io,express

# Do not use it breaks the script, very odd!
define colorecho
Expand All @@ -8,15 +8,18 @@ define colorecho
endef

run:all clean
@npm install socket.io
# @npm install socket.io express
node chat_server.js

all:
@ocamlfind ocamlc chat_server.ml -package $(pkgs) -linkpkg
@js_of_ocaml a.out -o chat_server.js

# clean:
# @rm -f chat_server2.cmi chat_server2.cmo \
# chat_server2.cmt a.out
clean:
@rm -f chat_server.cmi chat_server.cmo \
chat_server.cmt a.out
# chat_server.cmt a.out

.PHONY:clean
1 change: 1 addition & 0 deletions examples/chat_server.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(* Basically a translation of
http://arminboss.de/2013/tutorial-how-to-create-a-basic-chat-with-node-js/ *)

let program () =
let http = Nodejs.Http.require () in
let fs = Nodejs.Fs.require () in
Expand Down
18 changes: 18 additions & 0 deletions examples/chat_server2.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let program () =
let express = Express.require () in
let app = express##call() in
let server =
(Nodejs.Http.require ())##createServer_withapp app
in
let io = (Socket_io.require ())##call_arg server in
let port = 8080 in
server##listen port (Js.wrap_callback (fun () -> print_endline "Server Running"));
app##use (Js.wrap_callback begin fun req res ->
print_endline "serving"
end)

let run p =
ignore (p ())

let () =
run program
9 changes: 5 additions & 4 deletions examples/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
<head>
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
<script type="text/javascript">
var socketio = io.connect("127.0.0.1:8080");
socketio.on("message_to_client", function(data) {
var socketio = io.connect("127.0.0.1:8080");
socketio.on("message_to_client", function(data) {

document.getElementById("chatlog").innerHTML = ("<hr/>" +
data['message'] + document.getElementById("chatlog").innerHTML);
document.getElementById("chatlog").innerHTML =
("<hr/>" + data['message'] +
document.getElementById("chatlog").innerHTML);
});
function sendMessage() {
var msg = document.getElementById("message_input").value;
Expand Down
14 changes: 12 additions & 2 deletions src/http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,30 @@ let string_of_method = function
| Unsubscribe -> Js.string "unsubscribe"

class type incoming_message = object

method httpVersion : Js.js_string Js.readonly_prop

end

class type server_response = object

method url : Js.js_string Js.readonly_prop
(** Be sure to be giving writeHead a JS Object and nothing else *)
method writeHead : int -> Js.Unsafe.any -> unit Js.meth
method end_ : unit -> unit Js.meth
method end_data : Js.js_string Js.t -> unit Js.meth

end

class type client_request = object

method flush_headers : unit -> unit Js.meth
method write : Js.js_string -> Js.json -> unit Js.meth

end

class type server = object

(** Give a port and callback, get the handle of the server back *)
method listen : int -> (unit -> unit) Js.callback -> server Js.t Js.meth
(** Just tell the server to listen on this port and callback,
Expand All @@ -86,11 +93,14 @@ class type server = object
method address :
unit -> <address: Js.js_string Js.t Js.readonly_prop;
family : Js.js_string Js.t Js.readonly_prop;
port: Js.js_string Js.t Js.readonly_prop> Js.t Js.meth
port: Js.js_string Js.t Js.readonly_prop> Js.t Js.meth

end

class type http = object
method methods : Js.js_string Js.js_array Js.readonly_prop

method methods : Js.js_string Js.js_array Js.t Js.readonly_prop
method createServer_withapp : 'a Js.t -> server Js.t Js.meth
method createServer :
(incoming_message Js.t -> server_response Js.t -> unit) Js.callback ->
server Js.t Js.meth
Expand Down
4 changes: 2 additions & 2 deletions src/nodejs_globals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ let require (s : Js.js_string Js.t) =

let console : Js.Unsafe.any Js.t = Js.Unsafe.global##.console

let __filename : Js.js_string Js.t = Js.Unsafe.global##.__filename
let __filename () : Js.js_string Js.t = Js.Unsafe.global##.__filename

let __dirname : Js.js_string Js.t = Js.Unsafe.global##.__dirname
let __dirname () : Js.js_string Js.t = Js.Unsafe.global##.__dirname

0 comments on commit 6662f25

Please sign in to comment.