Skip to content

Instantly share code, notes, and snippets.

@suissa
Created October 15, 2016 20:11
Show Gist options
  • Save suissa/77ddfda6d3c6456336181cf3ade225c7 to your computer and use it in GitHub Desktop.
Save suissa/77ddfda6d3c6456336181cf3ade225c7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Workshop Socket.io - Events</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<h1>socket.io - Events</h1>
<p id="console"></p>
<input type="text" id="message">
<button id="sendmsg">ENVIAR</button>
<p id="mensagens">
</p>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
const socket = io.connect('http://localhost:8080');
const html = document.getElementById('console')
const message = document.getElementById('message')
const send = document.getElementById('sendmsg')
const mensagens = document.getElementById('mensagens')
send.addEventListener('click', function() {
let msg = message.value
console.log('msg:', msg)
sendMSG(msg);
$('#mensagens').append(msg + "<br />")
})
function sendMSG (msg) {
socket.emit('message', msg);
}
socket.on('connect', function(){
console.log('connectou')
const nick = prompt('Qual seu nick?')
socket.emit('adduser', nick)
html.innerHTML += 'connectou \r\n'
socket.emit('connect-client', Date.now());
// socket.on('')
});
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment