Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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
Socket.io socketio中类值和局部值之间的差异_Socket.io_This_Emit - Fatal编程技术网

Socket.io socketio中类值和局部值之间的差异

Socket.io socketio中类值和局部值之间的差异,socket.io,this,emit,Socket.io,This,Emit,由于某些原因,此代码不起作用 function process() { this.socket = null; } process.prototype = { makeCode : function() { this.socket = io.connect('/socket', {'force new connection': true}); this.socket.on('connect', function(){ this.socket.emit(

由于某些原因,此代码不起作用

function process() {
    this.socket = null;
}

process.prototype = {
makeCode : function() {
    this.socket = io.connect('/socket', {'force new connection': true});

    this.socket.on('connect', function(){
        this.socket.emit('ask');  //don't know it emits or not
        console.log('ask'); // printed 'ask'
    });

    this.socket.on('res', function(socketId) {
        console.log(socketId);  // never get here
    });
}
在服务器端:

.on('connection', function (socket) {

console.log("connected"); // print this line Only

socket.on('ask', function() { // never get here..
    console.log('res')
    socket.emit('res', socket.id);
})
但当我将套接字对象更改为局部值时,它工作得很好

    var socket = io.connect('/socket', {'force new connection': true});

    socket.on('connect', function(){
        socket.emit('ask'); // works well
        console.log('ask');
    });

    socket.on('res', function(socketId) {
        console.log(socketId); // works well
    });
我想使用套接字值作为类成员。我的代码有什么问题

------------------------添加代码---------------------------------------

require(['jquery','process'], function (jquery, process) {
            processer = new process();
            processer.makeCode();
        });

问题可能出在实例化
进程
对象的代码中,也可能出在调用
obj.makeCode()
的代码中。你能给我们看一下那段代码吗?我不认为这会造成问题,但我添加了那段代码..与你的问题无关,但你的意思是拼写它
jqeury
而不是
jQuery
?可能与你的问题有关:你的方法是
makeCode()
,但你的代码调用
makeQRCode()
,有什么原因吗,同样的方法。我刚刚更改了方法名,所以我的问题更清楚了。