2014-02-11 106 views
0

我有一個使用websockets的php項目啓動並運行,但僅以廣播的方式進行。後來(好吧...幾個小時前)我開始嘗試使用節點。 js和我很喜歡它。我想在我的項目中使用socket.io的'房間'功能。這裏是我迄今嘗試過的基本代碼片段:(test.js)。將socket.io集成到現有的php應用程序

var app = require('http').createServer() 
, io = require('socket.io').listen(app) 
, fs = require('fs'); 
app.listen(1234); 
//var io = require('socket.io').listen(1234); 
console.log('IO Server running'); 
//***Redis client*** 
var redis = require('redis'); 
client = redis.createClient(); 
client2 = redis.createClient(); 
channel = process.env.CHANNELL || 'chatroom'; 
client.on('error', function(err){ 
     console.log("Error ;" + err); 
     }); 
client.on('message', function(channel, message){ 
     console.log(message); 
     }); 
client.on('subscribe', function(channel){ 
       console.log('Client has subscribed'); 
     }); 

//***Event listeners*** 
//***onopen*** 
io.on('connection', function(socket){ 
    socket.emit('message', {'message': 'hello world'}); 
    console.log('New Connection accepted'); 
    client.subscribe(channel); 
    }); 
    //***onmessage*** 

    //***onclose*** 

我知道代碼是如何可笑的原始和幼稚上方出現,但很好,它已經只有幾個小時... (test.html中)--->服務與Apache,從XAMPP項目目錄

<html> 
<head> 
<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript" src="http://localhost:1234/socket.io/socket.io.js"></script>   
<script type="text/javascript"> 
var socket = io.connect(); 
</script> 
</head> 
<title>Primitive websocket in node.js</title> 

服務器盡職盡責開始,沒有任何錯誤,在控制檯:

info -socket.io started. 

然而,在客戶端有一個錯誤的錯誤:

GET http://localhost/socket.io/1/?t=1392148871226 404 (Not Found) socket.io.js:1659 

我已閱讀現有的文獻在線在這個特別的錯誤,但沒有一個似乎helpful.it看來我test.js服務器還必須爲test.html文件(或一些與不同的端口做)。否則404錯誤。是否有可能,我想在這裏做什麼? 如何讓錯誤消失? 任何建議/幫助將不勝感激。我的問題是或多或少像this question

回答

0

在主目錄中創建一個目錄'/socket.io'。從安裝的socket.io模塊中找到'socket.io.js'文件,並將其放入'/socket.io'中。 更改相關的代碼到:

<script src="socket.io/socket.io.js"></script> 

我有類似的問題。但改變後,它的作品。現在我有另一個問題可以在這裏看到:Integrating node.js, socket.io and php web page

相關問題