NodeChat

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 2b802407a67d2d3c14c4167e3e52d12b9ba49037
Author: Guillermo Rauch <rauchg@gmail.com>
Date:   Wed, 28 May 2014 09:31:39 -0700

initial commit

Diffstat:
A.gitignore | 1+
Aindex.html | 35+++++++++++++++++++++++++++++++++++
Aindex.js | 17+++++++++++++++++
Apackage.json | 9+++++++++
4 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/index.html b/index.html @@ -0,0 +1,35 @@ +<!doctype html> +<html> + <head> + <title>Socket.IO chat</title> + <style> + * { margin: 0; padding: 0; box-sizing: border-box; } + body { font: 13px Helvetica, Arial; } + form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } + form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } + form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } + #messages { list-style-type: none; margin: 0; padding: 0; } + #messages li { padding: 5px 10px; } + #messages li:nth-child(odd) { background: #eee; } + </style> + </head> + <body> + <ul id="messages"></ul> + <form action=""> + <input id="m" autocomplete="off" /><button>Send</button> + </form> + <script src="/socket.io/socket.io.js"></script> + <script src="http://code.jquery.com/jquery-1.11.1.js"></script> + <script> + var socket = io(); + $('form').submit(function(){ + socket.emit('chat message', $('#m').val()); + $('#m').val(''); + return false; + }); + socket.on('chat message', function(msg){ + $('#messages').append($('<li>').text(msg)); + }); + </script> + </body> +</html> diff --git a/index.js b/index.js @@ -0,0 +1,17 @@ +var app = require('express')(); +var http = require('http').Server(app); +var io = require('socket.io')(http); + +app.get('/', function(req, res){ + res.sendfile('index.html'); +}); + +io.on('connection', function(socket){ + socket.on('chat message', function(msg){ + io.emit('chat message', msg); + }); +}); + +http.listen(3000, function(){ + console.log('listening on *:3000'); +}); diff --git a/package.json b/package.json @@ -0,0 +1,9 @@ +{ + "name": "socket-chat-example", + "version": "0.0.1", + "description": "my first socket.io app", + "dependencies": { + "express": "4.3.1", + "socket.io": "1.0.0" + } +}