Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript socket.io已连接到late_Javascript_Node.js_Socket.io - Fatal编程技术网

Javascript socket.io已连接到late

Javascript socket.io已连接到late,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我有一个Nodejs项目 但是socket.io连接太晚了 其第一个已打开,socket.io未连接。10-15秒后,Socket.io几乎已连接 我的服务器JS: var io = require('socket.io').listen(8080); function getUsernames(room) { var clients = io.sockets.clients(room); client=new Array(); clients.forEach

我有一个Nodejs项目

但是socket.io连接太晚了

其第一个已打开,socket.io未连接。10-15秒后,Socket.io几乎已连接

我的服务器JS:

var io = require('socket.io').listen(8080);


function getUsernames(room) {
    var clients = io.sockets.clients(room);
        client=new Array();
    clients.forEach(function(_client) {

                client.push({username:_client.username,room:_client.room})
    });

    return client;
}

                io.sockets.on('connection', function(client){

                client.on('login', function(username, room){
                        client.username = username;
                        client.room = room;
                        client.join(client.room);
                        io.sockets.to(client.room).emit('updateuser', getUsernames(client.room));
                });

                        client.on('mesajgonder', function(data){
                                client.emit('mesajgitti', data)
                                client.broadcast.emit('mesajgitti', data)
                        });
                        client.on('disconnect', function(){
                                var eskiroom = client.room;
                                client.leave(client.room);
                                io.sockets.to(eskiroom).emit('updateuser', getUsernames(eskiroom));
                        });
});
var socket;
        var room = 'room8';
        var username = 'username';
        var socket = io.connect("http://localhost:8080");
        $(".text_metin").append('<span><p>Welcome to</p> <b>`'+room+'`</b> write now!</span>');
                socket.on('connect', function(s){
                        socket.emit('login', username, room);
                        socket.on("mesajgitti", function(data){
                                $(".text_metin").append("<li id='chat_metin'>"  + data.mesaj + "</li>");

                        });
                        socket.on('updateuser', function(users){
                                $.each(users, function(i, user) {
                                        $("#user_liste").empty();
                                        $("#user_liste").append('<li><img src="images/1111.png" alt="" width="20" height="20" /><a href="#" title="Coming Soon">'+user.username+'</a></li>');

                                });

                        });
                });
                        $("#mesajGonder").click(function(){
                                var mesaj = $("#writechat").val();
                                if (!mesaj){
                                return;
                                }
                                socket.emit('mesajgonder',{
                                        'mesaj' : mesaj,
                                });
                                $("#mesajinput").val("");
                        });
                        $(".tags li a").click(function(){
                var demo = $(this).text();
                 $("#interested").val($("#interested").val()+','+demo);
                });
                $("#mesajGonder").click(function(){
                $("#writechat").val('');
                });
我的客户JS:

var io = require('socket.io').listen(8080);


function getUsernames(room) {
    var clients = io.sockets.clients(room);
        client=new Array();
    clients.forEach(function(_client) {

                client.push({username:_client.username,room:_client.room})
    });

    return client;
}

                io.sockets.on('connection', function(client){

                client.on('login', function(username, room){
                        client.username = username;
                        client.room = room;
                        client.join(client.room);
                        io.sockets.to(client.room).emit('updateuser', getUsernames(client.room));
                });

                        client.on('mesajgonder', function(data){
                                client.emit('mesajgitti', data)
                                client.broadcast.emit('mesajgitti', data)
                        });
                        client.on('disconnect', function(){
                                var eskiroom = client.room;
                                client.leave(client.room);
                                io.sockets.to(eskiroom).emit('updateuser', getUsernames(eskiroom));
                        });
});
var socket;
        var room = 'room8';
        var username = 'username';
        var socket = io.connect("http://localhost:8080");
        $(".text_metin").append('<span><p>Welcome to</p> <b>`'+room+'`</b> write now!</span>');
                socket.on('connect', function(s){
                        socket.emit('login', username, room);
                        socket.on("mesajgitti", function(data){
                                $(".text_metin").append("<li id='chat_metin'>"  + data.mesaj + "</li>");

                        });
                        socket.on('updateuser', function(users){
                                $.each(users, function(i, user) {
                                        $("#user_liste").empty();
                                        $("#user_liste").append('<li><img src="images/1111.png" alt="" width="20" height="20" /><a href="#" title="Coming Soon">'+user.username+'</a></li>');

                                });

                        });
                });
                        $("#mesajGonder").click(function(){
                                var mesaj = $("#writechat").val();
                                if (!mesaj){
                                return;
                                }
                                socket.emit('mesajgonder',{
                                        'mesaj' : mesaj,
                                });
                                $("#mesajinput").val("");
                        });
                        $(".tags li a").click(function(){
                var demo = $(this).text();
                 $("#interested").val($("#interested").val()+','+demo);
                });
                $("#mesajGonder").click(function(){
                $("#writechat").val('');
                });
var插座;
var room=‘room8’;
var username='username';
变量套接字=io.connect(“http://localhost:8080");
$(“.text_metin”).append(“欢迎来到”“+room+”'write now!”);
socket.on('connect',函数){
发出('login',用户名,房间);
socket.on(“mesajgitti”,函数(数据){
$(“.text\u metin”).append(“
  • ”+data.mesaj+”
  • ”; }); socket.on('updateuser',函数(用户){ $.each(用户,函数(i,用户){ $(“#用户_列表”).empty(); $(“#用户列表”).append(“
  • ”); }); }); }); $(“#mesajGonder”)。单击(函数(){ var mesaj=$(“#writechat”).val(); 如果(!mesaj){ 返回; } socket.emit('mesajgonder'{ “mesaj”:mesaj, }); 美元(“#美沙金put”).val(“”); }); $(“.tags li a”)。单击(函数(){ var demo=$(this.text(); $(“#感兴趣的”).val($(“#感兴趣的”).val()+,'+demo); }); $(“#mesajGonder”)。单击(函数(){ $(“#writechat”).val(“”); });

    我不用快递。谢谢你添加到第一篇文章中。请在你的文章中包含所有相关代码,不要只包含代码共享网站的链接。只有链接的帖子在堆栈交换上,因为你的帖子应该独立于任何其他资源;考虑一下如果代码共享网站在将来会发生什么情况!我补充说,但不允许。我不得不使用一个链接共享站点。如果Stack Overflow不允许你在问题中发布代码,并让你发布链接,这是一个bug,你可能想让开发人员注意到它;确保包括关于如何重现问题的详细步骤。