-
-
Save amichaelgrant/3865900 to your computer and use it in GitHub Desktop.
node.js, express.js and socket.io — basic server/client communication via interface
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 io = require('socket.io'); | |
var express = require('express'); | |
var app = express.createServer(); | |
var server = app.listen(8080); | |
var ioServer = io.listen(server); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
ioServer.sockets.on('connection', function (socket) { | |
socket.send('Welcome to the server. You are now, obviously, connected.'); | |
socket.on('message', function (message) { | |
console.log('Message Received: ', message); | |
socket.send('This is the server answering to your message!'); | |
}); | |
socket.on('hello', function(body) { | |
console.log('Hello event recognized. Body: ' + body); | |
socket.emit('hello', 'This is the server answering to your event.'); | |
}); | |
}); |
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
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$(function() { | |
var socket = io.connect(); | |
var $statusOutput = $('#status'); | |
socket.on('connect', function () { | |
$statusOutput.append('[connected]<br>'); | |
socket.on('message', function(message) { | |
$statusOutput.append('> [message]: ' + message + '<br>'); | |
}); | |
socket.on('hello', function(body) { | |
$statusOutput.append('> [hello]: ' + body + '<br>'); | |
}); | |
socket.on('disconnect', function() { | |
$statusOutput.append('[disconnected]'); | |
}); | |
}); | |
$('#new-message').on('submit', function(event) { | |
event.preventDefault(); | |
var $messageInput = $(this).children('#message-input'); | |
var message = $messageInput.val(); | |
socket.send(message); | |
$messageInput.val(''); | |
$statusOutput.append('< [message]: ' + message + '<br>'); | |
}); | |
$('#emit-event').on('submit', function(event) { | |
event.preventDefault(); | |
var eventName = $(this).children('#emit-event-name').val(); | |
var eventBody = $(this).children('#emit-event-body').val(); | |
socket.emit(eventName, eventBody); | |
$statusOutput.append('< [' + eventName + ']: ' + eventBody + '<br>'); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="status"> | |
</div> | |
<p>Send a message to the server:</p> | |
<p> | |
<form id="new-message"> | |
<label for="message-input">Message:</label> | |
<input type="text" name="message-input" id="message-input"> | |
<input type="submit"> | |
</form> | |
</p> | |
<p>Emit an event to the server:</p> | |
<p> | |
<form id="emit-event"> | |
<label for="emit-event-name">Event:</label> | |
<input type="text" name="emit-event-name" id="emit-event-name"> | |
<label for="emit-event-body">Body:</label> | |
<input type="text" name="emit-event-body" id="emit-event-body"> | |
<input type="submit"> | |
</form> | |
</p> | |
</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": "sockit", | |
"version": "0.0.1", | |
"dependencies": { | |
"express": "3.0.x", | |
"socket.io": "0.9.6" | |
}, | |
"engines": { | |
"node": "0.8.1", | |
"npm": "1.1.35" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment