我使用socket.io创建了一个简单的聊天室。我在index.html中有这些脚本:
var socket = io.connect('http://imageworkz.asia:8080');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', prompt("What's your name?"));
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});
// listener, whenever the server emits 'updateusers', this updates the username list
socket.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
socket.emit('sendchat', message);
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
当我将连接更改为http://localhost:8080并在控制台中使用'node app.js'命令启动它时,它工作正常,但当我上传并将其更改为http://imageworkz.asia:8080时,它无效无论何时我去网址:http://imageworkz.asia:8080。我是否遗漏了某些东西,或者在上传时我还应该做些什么来使它工作?或者我会错误的网址?谢谢!
答案 0 :(得分:0)
尝试将您的node.js版本更新到网上的最新版本(http://imageworkz.asia:8080)。
同时检查网络上是否安装了所有必需的节点模块,如果需要,更改逻辑,以便您不需要prompt()来传输消息。
答案 1 :(得分:0)
我不完全确定,但我认为这应该可行:
var socket = io.connect('http://imageworkz.asia'); // on connection to server, ask for user's name with an anonymous callback socket.on('connect', function(){ // call the server-side function 'adduser' and send one parameter (value of prompt) socket.emit('adduser', prompt("What's your name?")); }); // listener, whenever the server emits 'updatechat', this updates the chat body socket.on('updatechat', function (username, data) { $('#conversation').append(''+username + ': ' + data + '
'); }); // listener, whenever the server emits 'updateusers', this updates the username list socket.on('updateusers', function(data) { $('#users').empty(); $.each(data, function(key, value) { $('#users').append('' + key + ''); }); }); // 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 socket.emit('sendchat', message); }); // when the client hits ENTER on their keyboard $('#data').keypress(function(e) { if(e.which == 13) { $(this).blur(); $('#datasend').focus().click(); } }); });
它只是删除 :8080