Created
September 4, 2014 03:48
-
-
Save cayasso/5de6294e9ee784397c1c to your computer and use it in GitHub Desktop.
Socket.IO Simple Test
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> | |
<head> | |
<title>Chat</title> | |
</head> | |
<body> | |
<form id="formDatos"> | |
<input type="text" id="dato" placeholder="Escriba el texto" required> | |
<input type="submit" value="enviar"> | |
</form> | |
<div id="recibir"></div> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="http://localhost:3000/socket.io/socket.io.js"></script> | |
<script> | |
var websocket = io.connect("localhost:3000"); | |
$(document).on("ready",inicio); | |
function inicio(){ | |
$("#formDatos").on("submit",enviarDatos); | |
websocket.on("datosServidor",recibirDatos); | |
} | |
function enviarDatos(e){ | |
e.preventDefault(); | |
var datos = $("#formDatos").find("#dato").val(); | |
websocket.emit("datosCliente",datos); | |
console.log('Datos eviados', datos); | |
} | |
function recibirDatos(data){ | |
console.log('Datos recibidos', data); | |
$("#recibir").append(data + '<br>'); | |
$("#formDatos").find("#dato").val(""); | |
} | |
</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", | |
"version": "0.0.1", | |
"dependencies": { | |
"express": "^4.8.7", | |
"socket.io": "1.0.6" | |
} | |
} |
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
// Importing required modules | |
var http = require('http'); | |
var express = require('express'); | |
var SocketIO = require('socket.io'); | |
// Set default server port | |
var port = process.env.PORT || 3000; | |
// Create an express application | |
var app = express(); | |
// Create a server attaching the express application | |
var server = http.Server(app) | |
// Attaching server to Socket IO. | |
var io = SocketIO(server) | |
server.listen(port, function listenCallback() { | |
console.log('Server listening on port %d', port); | |
}); | |
// Serve static file like index.html | |
app.use(express.static(__dirname)); | |
io.sockets.on("connection", arranque); | |
function arranque(socket){ | |
socket.on("datosCliente", enviarDatos); | |
} | |
function enviarDatos(data){ | |
io.sockets.emit("datosServidor", data); | |
} | |
console.log("Corriendo"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment