Last active
September 10, 2017 08:24
-
-
Save chathuranga94/956769374268ba6bc8c63961354f5cb1 to your computer and use it in GitHub Desktop.
SocketIO-v2-Chat
This file contains hidden or 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> | |
<body> | |
<form action=""> | |
<input id="m" autocomplete="off" /><button>Send</button> | |
</form> | |
<ul id="messages"></ul> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.0/socket.io.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> | |
<script> | |
var socket = io.connect('http://localhost:3000'); | |
$('form').submit(function(){ | |
//$('#messages').append($('<li>').text($('#m').val())); | |
socket.emit('sending', $('#m').val()); | |
$('#m').val(''); | |
return false; | |
}); | |
socket.on('recieve', function(msg){ | |
$('#messages').append($('<li>').text(msg)); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or 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 app = require('express')(); | |
var server = require('http').Server(app); | |
var io = require('socket.io')(server); | |
io.on('connection', function(socket){ | |
console.log('a user connected'); | |
socket.on('sending', function(data){ | |
console.log(data); | |
io.emit('recieve', data); | |
if(data=="exit"){ | |
socket.disconnect( console.log('sender disconnected')); | |
} | |
}); | |
}); | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
server.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment