Skip to content

Instantly share code, notes, and snippets.

@cs8425
Last active November 23, 2015 17:47
Show Gist options
  • Save cs8425/2f4b936cfd30d4a68b02 to your computer and use it in GitHub Desktop.
Save cs8425/2f4b936cfd30d4a68b02 to your computer and use it in GitHub Desktop.
/*
"name": "express",
"version": "4.12.3",
"name": "socket.io",
"version": "1.3.5",
*/
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var serveStatic = require('serve-static');
//var serveIndex = require('serve-index');
var server_port = 8081;
app.use(express.static(__dirname + '/index.html'));
app.use(serveStatic('public'));
var i = 0;
io.on('connection', function (socket) {
i++;
console.log('onlines', i, socket.id);
io.emit('debug', 'Hi~' + i + ' : ' + socket.id);
socket.on('error', function (e) {
console.log('ws error', e);
});
socket.on('disconnect', function () {
i--;
io.emit('debug', 'user disconnected: ' + socket.id);
console.log('onlines', i);
});
socket.on('debug', function (data) {
console.log('debug', data);
});
});
server.listen(server_port, function(){
console.log('Listening on port %d', server_port);
});
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8081');
socket.on('debug', function (data) {
console.log(data);
socket.emit('debug', { my: data });
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment