0

このコードは、何らかの理由で機能していません。

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();
        });
4

0 に答える 0