gpt4 book ai didi

node.js - socket.io 和express 3错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:33 25 4
gpt4 key购买 nike

运行 app.js 时没有出现错误,但无法获得输入用户名的 JavaScript 提示。我只能加载chat.html,其他的都不起作用。我认为问题可能是:

io = require('socket.io').listen(server);或

server.listen(80);

还在网络选项卡中我得到: ?t=1394734845750/socket.io/1得到(失败的)净::ERR_CONNECTION_REFUSEDsocket.io.js:1659 红色。

app.js

var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);

server.listen(80);

var fs = require('fs');


// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/chat.html');
});

// usernames which are currently connected to the chat
var usernames = {};

function check_key(v)
{
var val = '';

for(var key in usernames)
{
if(usernames[key] == v)
val = key;
}
return val;
}

io.sockets.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] = socket.id;
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo to client their username
socket.emit('store_username', username);
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected: ' + socket.id);
// 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');
});

// when the user sends a private msg to a user id, first find the username
socket.on('check_user', function(asker, id){
//console.log("SEE: "+asker); console.log(id);
io.sockets.socket(usernames[asker]).emit('msg_user_found', check_key(id));
});

// when the user sends a private message to a user.. perform this
socket.on('msg_user', function(usr, username, msg) {
//console.log("From user: "+username);
//console.log("To user: "+usr);
//console.log(usernames);
io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg);

fs.writeFile("chat_data.txt", msg, function(err) {
if(err) {
console.log(err);
} /*else {
console.log("The file was saved!");
}*/
});
});


});

chat.html

<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 type="text/javascript">
var my_username = '';
function send_individual_msg(id)
{
//alert(id);
//alert(my_username);
socket.emit('check_user', my_username, id);
//socket.emit('msg_user', id, my_username, prompt("Type your message:"));
}

var socket = io.connect('http://localhost:8008');

// 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 'msg_user_handle', this updates the chat body
socket.on('msg_user_handle', function (username, data) {
$('#conversation').append('<b>'+username + ':</b> ' + data + '<br>');
});

// listener, whenever the server emits 'msg_user_found'
socket.on('msg_user_found', function (username) {
//alert(username);
socket.emit('msg_user', username, my_username, prompt("Type your message:"));
});

// 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 'store_username', this updates the username
socket.on('store_username', function (username) {
my_username = username;
});

// listener, whenever the server emits 'updateusers', this updates the username list
socket.on('updateusers', function(data) {
//alert(data);
//console.log(data);
$('#users').empty();
$.each(data, function(key, value) {
$('#users').append('<div style="cursor:pointer;" onclick="send_individual_msg(\''+value+'\')">' + key + '</div>');
});
});

// on load of page
$(function(){
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
if(message == '' || jQuery.trim(message).length == 0)
return false;
$('#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();
$('#datasend').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:550px;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>

最佳答案

您的服务器正在监听端口 80,但您的套接字正在连接到端口 8008。更改其中之一,使它们位于同一端口上。

关于node.js - socket.io 和express 3错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com