Permalink
Browse files

better example code.

  • Loading branch information...
1 parent f6d334a commit 05a5026deb931af7ff27e375bbcb91ec3f072529 Tokuhiro Matsuno committed Sep 20, 2012
Showing with 20 additions and 4 deletions.
  1. +20 −4 eg/socketio.rb
View
@@ -7,16 +7,29 @@
</head>
<body>
<h1>This is a example script for node-mruby + socket.io</h1>
+<form id="chatform">
+ <input type="text" id="msg">
+ <input type="submit">
+</form>
<div id="log">
</div>
<script src="/socket.io/socket.io.js"></script>
+<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script>
var socket = io.connect("http://localhost:8080");
- socket.on("news", function (data) {
+ console.log("Ready");
+ var logElem = $("#log");
+ socket.on("chat", function (data) {
console.log(data);
- document.getElementById("log").innerHTML += "<div>" + JSON.stringify(data) + "</div>";
+ logElem.prepend($("<div>" + data["332"] + "</div>"));
socket.emit("my other event", { my: "data" });
});
+ $("#chatform").submit(function () {
+ socket.emit("chat", {msg: $("#msg").val()});
+ logElem.prepend($("<div>" + $("#msg").val() + "</div>"));
+ $("#msg").val("");
+ return false;
+ });
</script>
</body>
</html>
@@ -30,8 +43,11 @@
app.listen(8080)
io.sockets.on('connection') {|socket|
- console.log("On connection");
- socket.emit('news', {hello: 'world'})
+ socket.emit('chat', {msg: 'initialized'})
+ socket.on('chat') {|data|
+ p data.msg
+ socket.broadcast.emit('chat', {msg: data.msg})
+ }
socket.on('my other event') {|data|
p data
}

0 comments on commit 05a5026

Please sign in to comment.