Created
April 11, 2016 10:25
-
-
Save spddl/0dc3e835b61d67bd57a48a335b8a9e52 to your computer and use it in GitHub Desktop.
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 express = require('express'), | |
mysql = require('mysql'), | |
socket = require('socket.io'); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// | |
// ExpressJS | |
// | |
/////////////////////////////////////////////////////////////////////////////////////// | |
var app = express() | |
.get('*', function (req, res){ | |
res.type('text/plain'); | |
res.send('Hello World'); | |
}).listen(80); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// | |
// MySql | |
// | |
/////////////////////////////////////////////////////////////////////////////////////// | |
var connection = mysql.createConnection({ // TODO: daten ergenzen | |
host : 'localhost', | |
user : 'me', | |
password : 'secret', | |
database : 'my_db' | |
}); | |
connection.connect(); | |
/////////////////////////////////////////////////////////////////////////////////////// | |
// | |
// Socket.io | |
// | |
/////////////////////////////////////////////////////////////////////////////////////// | |
var io = socket.listen(app); // Verbindung zum WebServer | |
io.sockets.on('connection', function(socket){ | |
console.log('[WS] client connected! - '+socket.handshake.headers.referer); // wird über eine Verbindung informieren und die URL woher diese kommt | |
socket.on('mysql', function(data){ | |
console.log('User: '+data.user); | |
// MySQL | |
connection.query(data.cmd, function(err, rows, fields) { | |
if (err) throw err; | |
console.log('The solution is: ', rows[0].solution); | |
// Optional | |
socket.emit('client', 'done'); // ein Erledigt per Websocket zurück da "etwas" an kam was nicht auf Fehler lief | |
}); | |
}) | |
}); |
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script> | |
<script> | |
var socket = io('http://localhost'); | |
function WebSocketTest(){ | |
socket.emit('mysql', { cmd: 'show databases;',user:'localhost' }); // socket.emit überträgt Daten "von hier" | |
socket.on('client', function (data) { // socket.on empfängt Daten | |
alert(data) // String und kein Objekt | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<a href="javascript:WebSocketTest()">Run WebSocket</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment