Javascript Socket.io在网络中不工作
我使用socket.io创建了一个简单的聊天室。我的index.html中有以下脚本:Javascript Socket.io在网络中不工作,javascript,jquery,html,websocket,socket.io,Javascript,Jquery,Html,Websocket,Socket.io,我使用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 f
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();
}
});
});
var socket=io.connect('http://imageworkz.asia:8080');
//在连接到服务器时,通过匿名回调请求用户名
socket.on('connect',function(){
//调用服务器端函数“adduser”并发送一个参数(prompt的值)
emit('adduser',prompt(“你叫什么名字?”);
});
//侦听器,无论何时服务器发出“updatechat”,都会更新聊天主体
socket.on('updatechat',函数(用户名、数据){
$(“#对话”).append(“+username+”:“+data+”
”);
});
//侦听器,每当服务器发出“updateusers”时,就会更新用户名列表
socket.on('updateusers',函数(数据){
$('#users').empty();
$。每个(数据、函数(键、值){
$(“#用户”).append(“”+key+“”);
});
});
//加载页面
$(函数(){
//当客户端单击SEND时
$('#datasend')。单击(函数(){
var message=$('#data').val();
$('数据').val('');
//告诉服务器执行“sendchat”并发送一个参数
发出('sendchat',消息);
});
//当客户端在键盘上按ENTER键时
$(“#数据”)。按键(功能(e){
如果(e.which==13){
$(this.blur();
$('#datasend')。焦点()。单击();
}
});
});
当我在控制台中使用“node app.js”命令将连接更改为并启动它时,它工作正常,但当我将其上载并更改为时,每当我转到url:,它都不工作。我是否遗漏了一些东西,或者在上传时我还需要做些什么才能让它正常工作?还是我去错网址了?谢谢 尝试将node.js版本更新为网络上的最新版本(http://imageworkz.asia:8080).
还要检查网络上是否安装了所有必要的节点模块,如果需要,请更改逻辑,以便不需要提示符()来传输消息。我不完全确定,但我认为这应该可以: 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(); } }); }); var socket=io.connect('http://imageworkz.asia'); //在连接到服务器时,通过匿名回调请求用户名 socket.on('connect',function(){ //调用服务器端函数“adduser”并发送一个参数(prompt的值) emit('adduser',prompt(“你叫什么名字?”); }); //侦听器,无论何时服务器发出“updatechat”,都会更新聊天主体 socket.on('updatechat',函数(用户名、数据){ $(“#对话”).append(“+username+”:“+data+”
”); }); //侦听器,每当服务器发出“updateusers”时,就会更新用户名列表 socket.on('updateusers',函数(数据){ $('#users').empty(); $。每个(数据、函数(键、值){ $(“#用户”).append(“”+key+“”); }); }); //加载页面 $(函数(){ //当客户端单击SEND时 $('#datasend')。单击(函数(){ var message=$('#data').val(); $('数据').val(''); //告诉服务器执行“sendchat”并发送一个参数 发出('sendchat',消息); }); //当客户端在键盘上按ENTER键时 $(“#数据”)。按键(功能(e){ 如果(e.which==13){ $(this.blur(); $('#datasend')。焦点()。单击(); } }); });
它只是删除:8080考虑编辑您的答案,以解释为什么答案中的代码“应该工作”。