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()
,有什么原因吗,同样的方法。我刚刚更改了方法名,所以我的问题更清楚了。