[英]NodeJS + Socket.IO + Websocket + Flash - transfer the project to a hosting
我使用NodeJS + Socket.IO + Websocket + Flash創建了一個簡單的實時游戲項目。 在我的PC(localhost)上一切正常。 暫時將項目放置在免費托管的cloudno.de上。 不工作。
我使用以下文件:server.js-文件服務器nodejs(此文件未更改用於托管,因為此端口(8275)是通過托管提名我的應用程序的):
var io = require('socket.io'),
http = require('http');
var fs = require('fs'),
util = require('util');
var url = require('url'),
path = require('path'),
mime = require('mime');
function findType(uri) {
var ext = uri.match(/\.\w+$/gi);
if (ext && ext.length > 0) {
ext = ext[0].split(".")[1].toLowerCase();
return mime.lookup(ext);
}
return undefined;
}
function sendError(code, response) {
response.writeHead(code);
response.end();
return;
}
var app = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
if (uri === '/') {
uri = '/index.html';
} else if (uri === '/server.js') {
sendError(404, response);
return;
}
var _file = path.join(process.cwd(), uri);
path.exists(_file, function(exists) {
if (!exists) {
sendError(404, response);
} else {
fs.stat(_file, function(err, stat) {
var file = __dirname + uri,
type = findType(uri),
size = stat.size;
if (!type) {
sendError(500, response);
}
response.writeHead(200, {'Content-Type':type + "; charset=utf-8", 'Content-Length':size});
console.log("START");
var rs = fs.createReadStream(file);
util.pump(rs, response, function(err) {
if (err) {
console.log("ReadStream, WriteStream error for util.pump");
response.end();
}
});
});
}
});
});
var socket = io.listen(app, {transports:['websocket', 'flashsocket', 'xhr-polling']}),
buffer = [],
MAXBUF = 1024,
json = JSON.stringify;
var clients = [];
clients.usernames = function(client) {
return client.username;
}
socket.sockets.on('connection', function(client) {
console.log("CONNECTED");
client.on('message', function(data) {
//skipped more line of code
client.on('disconnect', function() {
if (client.username) {
client.json.broadcast.send({announcement:(client.username)+' left game', id:(client.id)});
}
var pos = clients.indexOf(client);
if (pos >= 0) {
clients.splice(pos, 1);
}
});});
if (!module.parent) {
app.listen(8275);
console.log("Socket-Chat listening on port 8275.. Go to http://<this-host>:8275");
}
index.html-客戶端文件。 這是一些連接Websocket的代碼。
<script src="/socket.io/socket.io.js" charset="utf-8"></script>
<script type="text/javascript" src="web_socket.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
// Set URL of your WebSocketMain.swf here:
WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";
// Set this to dump debug message from Flash to console.log:
WEB_SOCKET_DEBUG = true;
// Everything below is the same as using standard WebSocket.
var ws;
function init() {
// Connect to Web Socket.
// Change host/port here to your own Web Socket server.
ws = new WebSocket("ws://myapp.cloudno.de");//on localhost i use "localhost:8275" and will be change before transfer
// Set event handlers.
ws.onopen = function() {
output("onopen");
};
ws.onmessage = function(e) {
// e.data contains received string.
output("onmessage: " + e.data);
};
ws.onclose = function() {
output("onclose");
};
ws.onerror = function() {
output("onerror");
};
}
function onSubmit() {
var input = document.getElementById("input");
// You can send message to the Web Socket using ws.send.
ws.send(input.value);
output("send: " + input.value);
input.value = "";
input.focus();
}
function onCloseClick() {
ws.close();
}
function output(str) {
var log = document.getElementById("log");
var escaped = str.replace(/&/, "&").replace(/</, "<").
replace(/>/, ">").replace(/"/, """); // "
log.innerHTML = escaped + "<br>" + log.innerHTML;
}
</script>
web_socket.js-腳本未更改,完全從項目中獲取: https : //github.com/gimite/web-socket-js
WebSocketMain.swf-此文件也未更改,同樣來自https://github.com/gimite/web-socket-js
SocketGame.swf-這是我的游戲示例https://github.com/simb/FlashSocket.IO的主文件。 這僅更改了一行:socket = new FlashSocket(“ myapp.cloudno.de”); //在localhost上,我使用“ localhost:8275”,並且在轉移到主機之前將被更改
您能告訴我是否更改了托管配置嗎? 作為參考,服務器記錄托管和本地主機。 這種差異立即可見,但是尚不清楚為什么會發生這種情況。
主機控制台日志:
12月27日09:19:49-Cloudnode包裝的腳本(30128)在2011年12月27日星期二09:19:49 GMT + 0100(UTC)[36minfo-[39m socket.io開始於12月27日09:19:49-[INFO ] Cloudnode偵聽端口:8275。Socket-Chat偵聽端口8275。。轉至http://:8275 START START START START [90mdebug-[39m提供了靜態內容/socket.io.js開始START [90mdebug-[39m客戶端授權[36minfo-[39m握手授權1357476841432378537
Localhost控制台日志:
C:\\ inetpub \\ wwwroot \\ 14l>節點server.js信息-socket.io已開始在端口8275上偵聽Socket-Chat。轉到http://:8275開始調試-提供靜態內容/socket.io.js開始開始調試-客戶端授權信息-握手授權3511308552126147045調試-設置請求GET /socket.io/1/flashsocket/3511308552126147045調試-設置客戶端的心跳間隔3511308552126147045調試-客戶端授權進行調試-flashsocket寫入1 ::已連接
啟動我的應用程序后,發生握手並且一切都停止了-連接失敗。 我改變了很多選擇-沒有任何幫助。
我懷疑存在問題或socket.io(但我只是復制了與計算機一起使用的模塊)或Flash安全策略。 但是在我的特定情況下如何使用它尚不清楚。 這是應該提供幫助的模塊(https://github.com/3rd-Eden/FlashPolicyFileServer),但是如何將其集成到我的項目中呢?
我將不勝感激。
Alessioalex釘牢了它。 您的客戶端代碼沒有利用socket.io來完成您想要做的flash socket / xhr的工作。 您確實想在客戶端上使用socket.io:
您可能遇到的另一個問題是主機上對WebSockets的支持不足。 Heroku目前正遭受此困擾,他們建議使用xhr滾動:
http://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
FlashSocketPolicy集成應該非常容易。 您只需要npm安裝模塊,將其導入到app.js中,然后開始監聽即可。 github repo中的示例非常簡單:
https://github.com/3rd-Eden/FlashPolicyFileServer/blob/master/examples/basic.js
希望所有這些對編碼有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.