Permalink
Browse files

initial commit.

  • Loading branch information...
0 parents commit 7ed563b29777319ce37ab473082d179c9954597b @okamuuu committed Feb 22, 2012
@@ -0,0 +1,23 @@
+var http = require('http');
+var spawn = require('child_process').spawn;
+
+http.createServer(function (request, response) {
+
+ response.writeHead(200, {
+ 'Content-Type': 'text/plain'
+ });
+
+ var tail_child = spawn('tail', ['-f', '/var/log/system.log']);
+
+ request.connection.on('end', function () {
+ console.log('end request');
+ tail_child.kill();
+ });
+
+ tail_child.stdout.on('data', function (data) {
+// console.log(data.toString());
+ response.write(data);
+ });
+
+}).listen(4000);
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,32 @@
+var http = require('http');
+var fs = require('fs');
+var util = require('util');
+
+var file_path = __dirname + '/cat.jpg';
+
+fs.stat(file_path, function (err, stat) {
+
+ if (err) {
+ throw err;
+ }
+
+ http.createServer(function (request, response) {
+
+ response.writeHead(200, {
+ 'Content-Type': 'image/jpeg',
+ 'Content-Lenth': stat.size
+ });
+
+ var rs = fs.createReadStream(file_path);
+
+ util.pump(rs, response, function (err) {
+ if (err) {
+ throw err;
+ }
+ });
+
+ }).listen(4000);
+
+});
+
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,37 @@
+var http = require('http');
+var fs = require('fs');
+var util = require('util');
+var step = require('step');
+
+var file_path = __dirname + '/cat.jpg';
+var file_size, file_content;
+
+step(
+ function get_file_size() {
+ fs.stat(file_path, this);
+ },
+ function store_file_size(err, stat) {
+ file_size = stat.size;
+ this();
+ },
+ function read_file_into_memory() {
+ fs.readFile(file_path, this);
+ },
+ function create_server(err, file_content) {
+
+ http.createServer(function (request, response) {
+
+ response.writeHead(200, {
+ 'Content-Type': 'image/jpeg',
+ 'Content-Lenth': file_size
+ });
+
+ response.end(file_content);
+
+ }).listen(4000);
+
+ }
+);
+
+
+
@@ -0,0 +1,40 @@
+var net = require('net');
+var carrier = require('carrier');
+
+var connections = [];
+
+net.createServer(function (conn) {
+
+ connections.push(conn);
+
+ conn.on('close', function () {
+ var pos = connections.indexOf(conn);
+ if (pos >= 0) {
+ connections.splice(pos, 1);
+ }
+ });
+
+ conn.write("Hello, welcome to this chat server!\n");
+ conn.write("Please input your user name\n");
+
+ var username;
+
+ carrier.carry(conn, function (line) {
+ if (!username) {
+ username = line;
+ conn.write("Hello " + username + "!\n");
+ return;
+ }
+
+ if (line == 'quit') {
+ conn.end();
+ return;
+ }
+
+ connections.forEach(function (one_connection) {
+ one_connection.write(line);
+ });
+ });
+
+}).listen(4000);
+
@@ -0,0 +1,40 @@
+var net = require('net');
+var carrier = require('carrier');
+
+var connections = [];
+
+net.createServer(function (conn) {
+
+ connections.push(conn);
+
+ conn.on('close', function () {
+ var pos = connections.indexOf(conn);
+ if (pos >= 0) {
+ connections.splice(pos, 1);
+ }
+ });
+
+ conn.write("Hello, welcome to this chat server!\n");
+ conn.write("Please input your user name\n");
+
+ var username;
+
+ carrier.carry(conn, function (line) {
+ if (!username) {
+ username = line;
+ conn.write("Hello " + username + "!\n");
+ return;
+ }
+
+ if (line == 'quit') {
+ conn.end();
+ return;
+ }
+
+ connections.forEach(function (one_connection) {
+ one_connection.write(line);
+ });
+ });
+
+}).listen(4000);
+
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>Chat</title>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
+ <script type="text/javascript">
+ $(document).ready(function() {
+ ws = new WebSocket("ws://localhost:8080");
+ ws.onmessage = function(event) {
+ var data = event.data.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
+ $('#log ul').append('<li>' + data + '</li>');
+ };
+
+ var entry_el = $('#entry');
+ entry_el.keypress(function(event) {
+ if (event.keyCode != 13) return;
+ var msg = entry_el.attr('value');
+ if (msg) {
+ ws.send(msg);
+ entry_el.attr('value', '');
+ window.scrollBy(0, 1000000000000000);
+ entry_el.focus();
+ }
+ });
+
+ });
+ </script>
+ <style type="text/css">
+ body {
+ background-color: #666;
+ color: fff;
+ font-size: 14px;
+ margin: 0;
+ padding: 0;
+ font-family: Helvetica, Arial, Sans-Serif;
+ }
+ #log {
+ margin-bottom: 100px;
+ width: 100%;
+ }
+ #log ul {
+ padding: 0;
+ margin: 0;
+ }
+ #log ul li {
+ list-style-type: none;
+ }
+ #console {
+ background-color: black;
+ color: white;
+ border-top:1px solid white;
+ position: fixed;
+ bottom: 0;
+ width: 100%;
+ font-size: 18px;
+ }
+ #console input {
+ width: 100%;
+ background-color: inherit;
+ color: inherit;
+ font-size: inherit;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Chat</h1>
+ <div id="log"><ul></ul></div>
+ <div id="console">
+ <input type="text" id="entry" />
+ </div>
+ </body>
+</html>
+
@@ -0,0 +1,33 @@
+var http = require('http'),
+ util = require('util'),
+ fs = require('fs'),
+ io = require('socket.io');
+
+var server = http.createServer(function (request, response) {
+ response.writeHead(200, {
+ 'Content-Type': 'text/html'
+ });
+
+ var rs = fs.createReadStream(__dirname + '/template.html');
+ util.pump(rs, response);
+});
+
+var socket = io.listen(server);
+
+socket.on('connection', function (client) {
+
+ client.send('Welcome to this socket.io chat server!');
+ client.send('Please input your username: ');
+
+ client.on('message', function (message) {
+ if (!username) {
+ username = message;
+ client.send('Welcome, ' + username + '!');
+ return;
+ }
+
+ socket.broadcast(username + ' sent: ' + message);
+ });
+});
+
+server.listen(4000);
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>Chat</title>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
+ <script type="text/javascript" src="/socket.io/socket.io.js"></script>
+ <script type="text/javascript">
+ $(document).ready(function() {
+ socket = io.Socket('localhost', {port: 4000});
+ socket.on('message', function(event) {
+ var data = event.data.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
+ $('#log ul').append('<li>' + data + '</li>');
+ window.scrollBy(0, 1000000000000000);
+ entry_el.focus();
+ });
+
+ var entry_el = $('#entry');
+ entry_el.keypress(function(event) {
+ console.log(event);
+ if (event.keyCode != 13) return;
+ var msg = entry_el.attr('value');
+ if (msg) {
+ socket.send(msg);
+ entry_el.attr('value', '');
+ }
+ });
+
+ });
+ </script>
+ <style type="text/css">
+ body {
+ background-color: #666;
+ color: fff;
+ font-size: 14px;
+ margin: 0;
+ padding: 0;
+ font-family: Helvetica, Arial, Sans-Serif;
+ }
+ #log {
+ margin-bottom: 100px;
+ width: 100%;
+ }
+ #log ul {
+ padding: 0;
+ margin: 0;
+ }
+ #log ul li {
+ list-style-type: none;
+ }
+ #console {
+ background-color: black;
+ color: white;
+ border-top:1px solid white;
+ position: fixed;
+ bottom: 0;
+ width: 100%;
+ font-size: 18px;
+ }
+ #console input {
+ width: 100%;
+ background-color: inherit;
+ color: inherit;
+ font-size: inherit;
+ }
+ </style>
+ </head>
+ <body>
+ <h1>Chat</h1>
+ <div id="log"><ul></ul></div>
+ <div id="console">
+ <input type="text" id="entry" />
+ </div>
+ </body>
+</html>
+
Oops, something went wrong.

0 comments on commit 7ed563b

Please sign in to comment.