0

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);
Henry Sachs
  • 192
  • 18
  • what error messages do you get in your **browsers developer tool console** – Jaromanda X Sep 15 '16 at 09:34
  • i dont really get an error. The events just doesnt get triggered – Henry Sachs Sep 15 '16 at 09:35
  • You should wrap your jQuery related stuff in `$( document ).ready()` - see [API](https://learn.jquery.com/using-jquery-core/document-ready/) – Mike Scotty Sep 15 '16 at 09:36
  • 1
    `dont really get an error` - are you sure? I can't even see in your code where you specify where to connect the "socket" – Jaromanda X Sep 15 '16 at 09:37
  • @mpf82 Why? The jQuery script is clearly after the elements it refers. – Teemu Sep 15 '16 at 09:37
  • @mpf82 that shouldn't really matter if the script tag is at the bottom of the page (unless socket.IO has some kind of dependency on the dom being completely loaded) – Timothy Groote Sep 15 '16 at 09:37
  • var socket = io.connect(); here i connect to the server and that emits the Welcome message which i get but when i click on a button nothing happens – Henry Sachs Sep 15 '16 at 09:38
  • The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs. Use $( document ).ready() Check: http://stackoverflow.com/questions/13062246/when-should-i-use-jquerys-document-ready-function – Aman Mohammed Sep 15 '16 at 09:39
  • `var socket = io.connect(); here i connect to the server` - is the server on the same domain/ip and port number that the page was "served" from? I'm assuming that what you've posted is the WHOLE code – Jaromanda X Sep 15 '16 at 09:42
  • yeah it is i can reach it via localhost 8080 – Henry Sachs Sep 15 '16 at 09:45
  • so i now edited my code and used the document ready function and it seems like my document never gets ready :D – Henry Sachs Sep 15 '16 at 09:48
  • So anybody got an idea why i get the alert but cant emit the events? – Henry Sachs Sep 15 '16 at 09:55
  • with no errors in the browsers developer tools console, or even network tab, it's a bit difficult – Jaromanda X Sep 15 '16 at 10:13
  • So maybe you could try it on your node js server and tell me if it works for you – Henry Sachs Sep 15 '16 at 10:17
  • I just did - and your code works fine - click a button, I get `move` `move2` `test` on the server as expected - with the appropriate values from the input fields – Jaromanda X Sep 15 '16 at 10:32
  • so it seems to be a user problem right? ^^ – Henry Sachs Sep 15 '16 at 10:39
  • I see the issue ... possibly ... I made one little change to your code because I didn't want to load jquery onto my server, so I used a cdn - specifficaly `//code.jquery.com/jquery-1.12.4.min.js` ... the reason your code doesn't work is because your "server" isn't "serving" a local copy of jquery, because you haven't set your server to do so!! The problem I now have is, that you SHOULD be getting errors in your browsers developer tools console - a point I made 1 hour ago, but you claim there is no error. If I use your code as is, I **do get an error** – Jaromanda X Sep 15 '16 at 10:45
  • `ReferenceError: $ is not defined` in firefox. `(index):29 Uncaught ReferenceError: $ is not defined` in Chrome – Jaromanda X Sep 15 '16 at 10:49
  • Yeah you are right that my server cant find the Jquery file. I included a online cdn and it works. But i'm honest the server doesn't prints any errors. Edit: http://imgur.com/a/lAFMg here a screenshot first startup without the online cdn second with it and the code works. But could you help me including a local cdn because later the programm shall work offline too. – Henry Sachs Sep 15 '16 at 11:25
  • And which do you use on Linux for Web developement under node.js and javascript? – Henry Sachs Sep 15 '16 at 11:46
  • @HenrySachs what Jaromanda X was asking you to see was if you get any errors on the Browser Console - Developer tools option on your web browser and not on the web server. From the image screen shot it looks like you were looking at the server console and not the browser control. Just press F12 on any browser and you should see the browser control. – Aman Mohammed Sep 15 '16 at 12:21

0 Answers0