我是node.js&的新成员。 socket.io和我试图用我的简单项目从数据库中获取数据。
我的index.html是socket.io与简单的ajax请求实时聊天的工作示例
<!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>
我的ajax.php
<?php
$data = "123aa";
echo json_encode($data);
?>
最后,但并非最不重要的是,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');
});
他们工作正常,但我无法找到为什么我无法访问我的ajax.php并从中获取数据
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 - 我试图从&t=1432217406964-0
删除url
,或者甚至找出我应该放在这里的内容,但我发现了什么。所以我不知道这是不对的
url: '/socket.io/ajax.php?EIO=3&transport=polling&t=1432217406964-0',
2 - 这是我的文件树
- chat-example-master/
------ index.html
------ index.js
------ ajax.php
3 - 我的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"
}
}
那么,我可以使用Ajax从我的数据库中获取数据,以及如何做到这一点?
(另外,如果有人可以在URL
中解释什么对&amp; l = xxxxxxxx-0起作用,那就太棒了)
PS:我使用Node.js v0.12.2和Socket.io v1.3.6
PS2:抱歉我的英语很糟糕
PS3:我认为我试图让一些事情变得不可能或者缺少一些非常愚蠢的东西
答案 0 :(得分:0)
我懂了!
这个简单的项目帮助我更多地了解了socket.io中node.js的工作原理以及我可以从数据库中获取数据(在这种情况下,没有Ajax http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/
由于