简体   繁体   中英

i can't get data from database with socket.io and node.js

I'm new at node.js & socket.io and I'm trying to get data from a database with my simple project.

My index.html is the working example by socket.io real-time chat with a simple ajax request

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });

     $.ajax({
      url: '/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0',
        type: 'GET',
        dataType: 'json',
        timeout: 10000,
        error: function (x,e) {
            if (x.status == 0 && e == 'timeout') {
                console.log('Timeout.');
            } else if (x.status == 404) {
                console.log('Error 404.');
            } else if (x.status == 500) {
                console.log('Interal Rrror.');
            } else if (e == 'parsererror') {
                console.log('Failed Request.');
            } else {
                console.log('Unknown error.' + x.responseText);
            }
        },
        success: function (data) {
           console.log(data);
        }
    });
    </script>
  </body>
</html>

My ajax.php

<?php
    $data = "123aa";
    echo json_encode($data);
?>

And at last, but not least, the index.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);



app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){

    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

They are working just fine, but i can't find out why i can't reach my ajax.php and get data from it

XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957690-0".16.Request.create @ socket.io-1.2.0.js:2Request @ socket.io-1.2.0.js:216.XHR.request @ socket.io-1.2.0.js:216.XHR.doPoll @ socket.io-1.2.0.js:217.Polling.poll @ socket.io-1.2.0.js:217.Polling.doOpen @ socket.io-1.2.0.js:213.Transport.open @ socket.io-1.2.0.js:112.Socket.open @ socket.io-1.2.0.js:1Socket @ socket.io-1.2.0.js:1Socket @ socket.io-1.2.0.js:13.Manager.open.Manager.connect @ socket.io-1.2.0.js:1Manager @ socket.io-1.2.0.js:1Manager @ socket.io-1.2.0.js:1lookup @ socket.io-1.2.0.js:1(anonymous function) @ (index):24
jquery-1.11.1.js:9631 XHR finished loading: GET "http://localhost:3000/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0".jQuery.ajaxTransport.send @ jquery-1.11.1.js:9631jQuery.extend.ajax @ jquery-1.11.1.js:9176(anonymous function) @ (index):34
(index):47 Failed Request.
socket.io-1.2.0.js:2 XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957755-1&sid=kPUxnMKOvT2kdHmLAAAO".16.Request.create @ socket.io-1.2.0.js:2Request @ socket.io-1.2.0.js:216.XHR.request @ socket.io-1.2.0.js:216.XHR.doPoll @ socket.io-1.2.0.js:217.Polling.poll @ socket.io-1.2.0.js:217.Polling.onData @ socket.io-1.2.0.js:2(anonymous function) @ socket.io-1.2.0.js:28.Emitter.emit @ socket.io-1.2.0.js:116.Request.onData @ socket.io-1.2.0.js:216.Request.onLoad @ socket.io-1.2.0.js:216.Request.create.xhr.onreadystatechange @ socket.io-1.2.0.js:2
socket.io-1.2.0.js:2 XHR finished loading: GET "http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1441116957858-2&sid=kPUxnMKOvT2kdHmLAAAO".

1 - I've tried to remove &t=1432217406964-0 from url , or even find out what should i put here, but i find out anything. So i don't know if this is right

url: '/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0',

2 - This is my file tree

- chat-example-master/  
------ index.html  
------ index.js  
------ ajax.php

3 - My package.json

{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "4.10.2",
    "socket.io": "1.2.0"
  }
}

So, can i use Ajax to get data from my database, and how to do it? (Also, if someone could explain what does work for &l=xxxxxxxx-0 in URL it would be great)

PS: I'm using Node.js v0.12.2 and Socket.io v1.3.6
PS2: Sorry for my terrible english
PS3: I think that i'm trying to make something impossible or missing something very stupid

I get it!

This simple project helped me to understand a little bit more about who socket.io an node.js works and who can I get data from my database (in this case, without Ajax http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM