简体   繁体   English

Socket.io:“无法读取未定义的属性'emit'”

[英]Socket.io : “Cannot read property 'emit' of undefined”

So I'm currently developping my website using socket.io. 因此,我目前正在使用socket.io开发我的网站。

Today, while I was implementing the sockets, I got a nice little error "Cannot read property 'emit' of undefined" I can not find what's wrong in my code so I hope you guys can help me. 今天,在实现套接字的过程中,我遇到了一个小错误“无法读取未定义的属性'emit'”,我找不到代码中的错误,希望大家能为我提供帮助。

App.js: App.js:

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

server.listen(port, function () {
  console.log('Listening at port %d', port);
});

io.sockets.on('connection', function (socket) {
    console.log('User connected');

    socket.on('create_socket', function(port){
    console.log(port);
});

Client Side: 客户端:

<div>
    <p class="connected" hidden>Connected port : 9999</p>
    <p class="connect_error" hidden><strong>Can not connect to socket on port 9999</strong></p>
</div>

<script>
    var socket = io.connect("http://localhost:9999");

    // If connection can not be established
    socket.on('connect_error', function(socket) {
        $('.connected').hide();
        $('.connect_error').show();
    });

    // Connection established
    socket.on('connect', function(socket) {
        $('.connect_error').hide();
        $('.connected').show();
       // socket.on('create_socket', null, <?= $port; ?> );
       socket.emit('create_socket', '<?= $port; ?>');
    });
</script>

Btw, all the socket.io/jquery/... includes are done correctly even if they doesn't appear on the code 顺便说一句,所有的socket.io/jquery / ...包含都正确完成,即使它们未出现在代码上

Thanks in advance 提前致谢

Client Code: 客户代码:

Change 更改

// Connection established
    socket.on('connect', function(socket) {

to

// Connection established
    socket.on('connect', function(data) {

final: 最后:

// Connection established
    socket.on('connect', function(data) {
        $('.connect_error').hide();
        $('.connected').show();
       console.log(data);
       socket.emit('create_socket', '<?= $port; ?>');
    });

Previously, you where overwriting the socket object. 以前,您在哪里覆盖套接字对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Socket.IO TypeError:无法读取未定义的属性“发出” - Socket.IO TypeError: Cannot read property 'emit' of undefined AngularJS Socket.io:无法读取未定义的属性“ emit” - AngularJS Socket.io: Cannot read property 'emit' of undefined Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined - Socket.io - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'emit' of undefined WebRTC,socket.io,node.js:无法读取未定义的属性&#39;emit&#39;? - WebRTC , socket.io, node.js: Cannot read property 'emit' of undefined? 使用React / Socket.IO无法读取未定义错误的属性&#39;emit&#39; - Cannot read property 'emit' of undefined error using React/Socket.IO Karma无法读取未定义的socket.io的属性&#39;prototype&#39;? - Karma Cannot read property 'prototype' of undefined socket.io? Node.js和Socket.io-无法读取未定义的属性“ on” - Node.js & Socket.io - Cannot read property 'on' of undefined Socket.io 未捕获类型错误:无法读取未定义的属性“应用” - Socket.io Uncaught TypeError: Cannot read property 'apply' of undefined Socket.io TypeError:无法读取未定义的属性“hteam” - Socket.io TypeError: Cannot read property 'hteam' of undefined Socket.io引发“ TypeError:无法读取未定义的属性&#39;push&#39;” - Socket.io raises “TypeError: Cannot read property 'push' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM