我正在使用命名空間來區分我的socket.io聊天應用程序的版本,並且遇到了「瀏覽器中無法顯示GET錯誤」的問題。當命名空間收到瀏覽器中顯示的「無法獲取錯誤」
我打算不斷更新我在基本socket.io教程中所做的聊天應用程序,並且我希望能夠隨時啓動它的任何版本。我將通過使用名稱空間來完成此操作。當我在位置myserverlocation/v0.0.1的瀏覽器中啓動我的應用程序以訪問我的應用程序的版本0.0.1時,出現錯誤,指出無法獲得'/v0.0.1'。
這是我的服務器代碼:
var app = require('express')(),
server = require('http').Server(app),
io = require('socket.io').listen(server),
chat = io.of('/v0.0.1');
server.listen(80);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
chat.on('connection', function (socket) {
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username) {
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function() {
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
});
這是我的客戶端代碼:
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect('myserverlocation');
var chat = socket.of('/v0.0.1');
// on connection to server, ask for user's name with an anonymous callback
chat.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
chat.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
chat.on('updatechat', function(username, data) {
$('#conversation').append('<b>' + username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
chat.on('updateusers', function(data) {
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div>' + key + '</div>');
});
});
// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click(function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
chat.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
我的聊天應用程序工作正常,沒有命名空間的使用,在myserverlocation /。我無法弄清楚爲什麼我不斷收到這個錯誤。經過一番調查後,我認爲我對io.of()的使用不正確,但似乎無法解決問題。我不確定我的問題在於服務器代碼,客戶端代碼還是兩者。
編輯:更多的調查之後,我想我的問題出在代碼的後續段(雖然我可能是錯誤的):
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
EDIT2:其實謊言在上面的代碼段的問題一樣。我應該一直髮送我的整個/聊天目錄作爲靜態內容,而不是使用res.sendfile()發送一個文件。我會正式回答我自己的問題,當stackoverflow讓我(我必須等待8個小時回答我自己的問題)。
不能完全肯定這一點,但它是可能的,它不喜歡你的命名空間中的點?無論哪種方式[Apigee建議](https://blog.apigee.com/detail/restful_api_design_tips_for_versioning):使用一個簡單的序號 - v1,v2等等。不要使用像v1.2這樣的點符號,因爲它暗示了一種不適合API的版本控制粒度 - 它是一個接口而不是實現。 – kentcdodds
我只是試圖將版本名稱更改爲v1,問題仍然存在。然而,我將在未來以簡單的方式接受你的建議。 – Zarrok
我解決了我自己的問題,但顯然我不能回答我自己的問題,直到它張貼大聲笑後8小時。我會盡快發佈我的答案,以防將來有其他人遇到問題。問題是我使用res.sendfile()。 – Zarrok