嘿伙计们我遇到了以下问题。我在这里得到了这个Html,应该连接到我的node.js服务器并触发事件无论如何网站连接但是当我点击一个按钮时他们不发出事件。所以我假设jquery有错误。
所以这是我的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>
和节点服务器
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);