Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

chat example at nws-examples #1

Open
wants to merge 1 commit into from

1 participant

Kensaku KOMATSU
Kensaku KOMATSU

Hi miksago

Thanks for your information!! I applogize my misunderstanding to make use of git.

I try to append chat examples w/ nws-memstore at nws-examples.

Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 30, 2010
Kensaku KOMATSU append chat example utilizing nws-memstore 3424671
This page is out of date. Refresh to see the latest.
75  chat/chat-server.js
... ...
@@ -0,0 +1,75 @@
  1
+var sys = require("sys")
  2
+  , http = require("http")
  3
+  , fs = require("fs")
  4
+  , path = require("path")
  5
+  , ws = require('node-websocket-server/lib/ws/server')
  6
+  , memstore = require('nws-memstore/lib/nws-memstore')
  7
+  , lport = 8000;
  8
+
  9
+var httpServer = http.createServer(function(req, res){
  10
+  if(req.method == "GET"){
  11
+    if( req.url.indexOf("favicon") > -1 ){
  12
+      res.writeHead(200, {'Content-Type': 'image/x-icon', 'Connection': 'close'});
  13
+      res.end("");
  14
+    } else {
  15
+      res.writeHead(200, {'Content-Type': 'text/html', 'Connection': 'close'});
  16
+      fs.createReadStream( path.normalize(path.join(__dirname, "chat.html")), {
  17
+        'flags': 'r',
  18
+        'encoding': 'binary',
  19
+        'mode': 0666,
  20
+        'bufferSize': 4 * 1024
  21
+      }).addListener("data", function(chunk){
  22
+        res.write(chunk, 'binary');
  23
+      }).addListener("end",function() {
  24
+        res.end();
  25
+      });
  26
+    }
  27
+  } else {
  28
+    res.writeHead(404);
  29
+    res.end();
  30
+  }
  31
+});
  32
+
  33
+
  34
+var server = ws.createServer({
  35
+  server: httpServer
  36
+});
  37
+
  38
+memstore.attachTo(server);
  39
+
  40
+server.addListener("listening", function(){
  41
+  sys.log("Listening for connections port "+lport+".");
  42
+});
  43
+
  44
+// Handle WebSocket Requests
  45
+server.addListener("connection", function(conn){
  46
+  conn.memstore.set("username", "user_"+conn.id);
  47
+
  48
+  conn.send("** Connected as: user_"+conn.id);
  49
+  conn.send("** Type `/nick USERNAME` to change your username");
  50
+
  51
+  conn.broadcast("** "+conn.memstore.get("username")+" connected");
  52
+
  53
+  conn.addListener("message", function(message){
  54
+    if(message[0] == "/"){
  55
+      // set username
  56
+      if((matches = message.match(/^\/nick (\w+)$/i)) && matches[1]){
  57
+        conn.memstore.set("username", matches[1]);
  58
+        conn.send("** you are now known as: "+matches[1]);
  59
+
  60
+      // get message count
  61
+      } else if(/^\/stats/.test(message)){
  62
+        conn.send("** you have sent "+conn.memstore.get("messages", 0)+" messages.");
  63
+      }
  64
+    } else {
  65
+      conn.memstore.incr("messages");
  66
+      server.broadcast(conn.memstore.get("username")+": "+message);
  67
+    }
  68
+  });
  69
+});
  70
+
  71
+server.addListener("close", function(conn){
  72
+  server.broadcast("<"+conn.id+"> disconnected");
  73
+});
  74
+
  75
+server.listen(lport);
93  chat/chat.html
... ...
@@ -0,0 +1,93 @@
  1
+<!doctype html>
  2
+<html>
  3
+  <head>
  4
+    <title>Chat demo</title>
  5
+		<style type="text/css">
  6
+#controls {
  7
+	position: fixed;
  8
+	bottom: 0;
  9
+	left: 0;
  10
+	right: 0;
  11
+	background: #fff;
  12
+	padding: 0.5em;
  13
+	display: block;
  14
+}
  15
+
  16
+#message {width: 80%;}
  17
+
  18
+#log {
  19
+  font-family: helvetica, arial, sans-serif;
  20
+  font-size: .8em;
  21
+  padding-bottom: 40px;
  22
+}
  23
+		</style>
  24
+		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  25
+  </head>
  26
+  <body>
  27
+		<div id="log"></div>
  28
+	  <div id="controls">
  29
+	    <hr />
  30
+	    <input type="text" id="message"/>
  31
+	    <input type="button" value="send" id="sendbtn" disabled="disabled">
  32
+		</div>
  33
+    <script type="text/javascript">
  34
+var conn;
  35
+$(function(){
  36
+  var history = [], idx = 0;
  37
+  $("#message").focus();
  38
+
  39
+  $(window).bind("focus", function(){
  40
+    $("#message").focus();
  41
+  });
  42
+
  43
+  var logpanel = $("#log");
  44
+
  45
+  function sendmsg(){
  46
+    if(conn && conn.readyState == 1){
  47
+      var msg = $("#message").val();
  48
+      history.unshift(msg);
  49
+      conn.send(msg);
  50
+      idx = 0;
  51
+      $("#message").val("");
  52
+    }
  53
+  };
  54
+
  55
+  $("#sendbtn").bind("click", function(){
  56
+    sendmsg();
  57
+  });
  58
+
  59
+  $("#message").bind("keydown", function(e){
  60
+    if(e.which == 13){
  61
+      sendmsg();
  62
+    }
  63
+  })
  64
+
  65
+  function scrollToBottom() {
  66
+      window.scrollBy(0, document.body.scrollHeight - document.body.scrollTop);
  67
+  };
  68
+
  69
+  function log(data){
  70
+    logpanel.append("<p>"+data+"</p>");
  71
+    scrollToBottom();
  72
+  };
  73
+
  74
+  if (window["WebSocket"]) {
  75
+    conn = new WebSocket("ws://"+document.location.host+"/");
  76
+
  77
+    conn.onmessage = function(evt) {
  78
+      log(evt.data);
  79
+    };
  80
+
  81
+    conn.onclose = function() {
  82
+      log("** you have been disconnected");
  83
+    };
  84
+
  85
+    conn.onopen = function(){
  86
+      log("** you have been connected");
  87
+      $("#sendbtn").removeAttr("disabled");
  88
+    }
  89
+  }
  90
+});
  91
+    </script>
  92
+  </body>
  93
+</html>
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.