1
嘗試將socket.io/nodejs集成到頁面中後,很久以來我都遇到了很多嘗試爲其設置服務器後端的問題。通過NVM安裝節點(節點版本0.12.1
,socket.io版本1.3.5
)socket.io.js返回一個404
看來,當我嘗試運行服務器(通過一個簡單的node receiver.js
),我無法連接到/socket.io/socket.io.js
在我index.php
文件,如下圖所示在提供的代碼:
的index.php(客戶端)
<!-- in html > head -->
<script src="/socket.io/socket.io.js"></script> <!-- Problem here -->
<script>
if (typeof io === 'undefined') {
console.error("Error connecting to nodejs backend! Expect some errors");
}
var socket = io();
</script>
<script src="/assets/js/selector.js"></script>
receiver.js(服務器端)
個console.log('loading requirements...');
var io = require('socket.io')(80);
var net = require('net');
var mysql = require('mysql2');
console.log('connecting to mysql...');
var connection = mysql.createConnection({
user: '', //removed
password: '', //removed
database: '', //removed
stream: net.connect(3306, "codelanx.com")
});
var sent = {};
console.log('listening...');
io.sockets.on('connection', function(socket) {
//socket.io methods, etc...
selector.js(客戶端)
//...
jQuery(document).ready(function($) {
socket.emit('requestName');
//...
}
在服務器端,所有日誌語句被印刷(它對listening...
)。我一直在以幾乎所有可以想象的方式配置require('socket.io')
這樣的呼叫線路,但仍然無法連接。我錯過了什麼嗎?
在正在從服務這個index.php文件?你試試io.on('connection'...) – 2015-03-31 13:55:08
@YakiKlein我的'index.php'位於沙盒子域的webroot('/ var/www/sandbox')中,並使用'io.on ('connection',// ...)'仍然返回404 for'/ socket.io/socket.io.js'(是'io.sockets'不再使用?) – Rogue 2015-03-31 14:07:38
嘗試降級到1.2,[1.3。 5在這裏創建了一些問題](http://stackoverflow.com/questions/29342449/send-data-to-selected-socket-id#comment46905125_29342449) – laggingreflex 2015-04-01 08:21:34