Last active
January 9, 2016 12:25
-
-
Save pahaz/c3b134743074c7e156fa to your computer and use it in GitHub Desktop.
socket-io chat example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Chat Example</title> | |
</head> | |
<body> | |
<script src="/socket.io/socket.io.js"></script> | |
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> | |
<script> | |
var socket = io.connect(); | |
var messages = []; | |
function do_on_connect () { | |
console.log('connect!'); | |
} | |
function do_on_message_from_server (msg) { | |
console.log('message!', msg); | |
messages.push(msg); | |
} | |
socket.on('connect', do_on_connect); | |
socket.on('message', do_on_message_from_server); | |
function send (msg) { | |
console.log('Sending message:', msg); | |
socket.emit('message', msg); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "chat-example", | |
"version": "0.0.0", | |
"description": "A chat example to showcase how to use `socket.io` with a static `express` server", | |
"main": "server.js", | |
"repository": "", | |
"author": "Pahaz Blinov <pahaz.blinov@gmail.com>", | |
"dependencies": { | |
"express": "~3.2.4", | |
"socket.io": "~0.9.14" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var path = require('path'); | |
// WS SERVER | |
var socketio = require('socket.io'); | |
// HTTP SERVER | |
var express = require('express'); | |
var router = express(); // создаем роутер | |
var server = http.createServer(router); // создаем HTTP сервер | |
var io = socketio.listen(server); // для обмена сообщениями с сервером | |
router.use(express.static(path.resolve(__dirname, '.'))); // будем отдавать файлы | |
var messages = []; | |
var clients = []; | |
var index = 0; | |
io.on('connection', function (client) { | |
for (var i = 0; i < messages.length; i++) { | |
client.emit('message', messages[i]); | |
} | |
clients.push(client); | |
client.on('disconnect', function () { | |
clients.splice(clients.indexOf(client), 1); | |
}); | |
client.on('message', function (data) { | |
if (!data) | |
return; | |
broadcast(data); // рассылаем всем клиентам | |
messages.push(data); | |
}); | |
}); | |
function broadcast(data) { | |
for (var i = 0; i < clients.length; i++) { | |
var client = clients[i]; | |
client.emit('message', data); | |
} | |
} | |
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){ | |
var addr = server.address(); | |
console.log("Chat server listening at", addr.address + ":" + addr.port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm install
для установки пакетовnode server.js
для запуска