0

This code is not working because of some reason.

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 the server side:

.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);
})

But when I change socket Object as local value, It works well.

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

I want to use socket value as class member. What is problem with my code?

------------------------added code---------------------------------------

require(['jquery','process'], function (jquery, process) {
            processer = new process();
            processer.makeCode();
        });
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DK2
  • 651
  • 1
  • 7
  • 34
  • The problem could be in the code that instantiates a `process` object or in the code that calls `obj.makeCode()`. Can you show us that code? – jfriend00 Jan 26 '15 at 09:21
  • I don't think it makes problem, but I added that code.. – DK2 Jan 26 '15 at 09:48
  • Not related to your problem, but do you really mean so spell it `jqeury` instead of `jQuery`? – jfriend00 Jan 26 '15 at 09:50
  • Possibly related to your problem: is there a reason your method is `makeCode()`, but your code calls `makeQRCode()`? – jfriend00 Jan 26 '15 at 09:51
  • oops, Its same method. I just changed method name, so my question is more clear. – DK2 Jan 26 '15 at 09:55
  • Why is it that you don't know if `this.socket.emit('ask');` works or not? Can't you see the `console.log('res')` on your server to see whether it is or isn't working? Also, the code that you say works use `tsocket.emit('ask');`, but the variable is `socket`. Why are those different? – jfriend00 Jan 26 '15 at 10:02
  • becuase It's not working from server side or client side. I think It's problem on client but not sure. tsocket is miss spell. sorry. – DK2 Jan 26 '15 at 10:13
  • What client-side `require()` library are you using? Are you 100% sure that the argument named `process` to your callback function is actually the `process` constructor? If you do `console.log(process)` in the `require()` callback, what does it show? If you put a `console.log("process constructor")` in the `process()` function body, does it get called? I'm suspecting there's a problem with the module definition that contains your `process()` function. – jfriend00 Jan 26 '15 at 10:17
  • I'm using require.js from www.requirejs.org. process is printed in callback function and "process constructor" is also printed out in process function. everything about construction works well. – DK2 Jan 26 '15 at 10:59

0 Answers0