Hey guys i got the following Problem. I got this Html right here which should connect to my node.js Server and trigger Events anyway the Website connects but when i click a button they dont emit the events. So i assume an error with the jquery.
so this is my HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Socket.io</title>
<script src="jquery-1.12.4.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<h1>Communicating with socket.io!</h1>
<input type="text" name="geschwindigkeit" id="v">
<input type="text" name="Neigung" id="acc">
<p><input type="button" value="Move" id="move" /></p>
<p><input type="button" value="Start" id="poke" /></p>
<p><input type="button" value="test" id="test" /></p>
<script>
var socket = io.connect();
// A dialog box is displayed when the server sends us a "message"
socket.on('message', function(message) {
alert('Nachricht: ' + message);
})
// When the button is clicked, a "message" is sent to the server
$('#poke').click(function () {
socket.emit('message', 'move2');
})
$('#test').click(function () {
socket.emit('message', 'test');
})
// When the button is clicked, a "message" is sent to the server
$('#move').click(function () {
var geschw = document.getElementById("v").value;
var neig = document.getElementById("acc").value;
socket.emit('move', 'move ' + geschw + ' ' + neig);
})
</script>
</body>
and the Node server
var http = require('http');
var fs = require('fs');
// Loading the file index.html displayed to the client
var server = http.createServer(function(req, res) {
fs.readFile('./index.html', 'utf-8', function(error, content) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(content);
});
});
// Loading socket.io
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
// When the client connects, they are sent a message
console.log('Client verbunden');
socket.emit('message', 'Willkommen');
// The other clients are told that someone new has arrived
socket.broadcast.emit('message', 'Another client has just connected!');
// When a "message" is received (click on the button), it's logged in the console
socket.on('message', function (message){
console.log(message);
});
socket.on('move', function(message){
console.log(message);
});
});
server.listen(8080);