由于某种原因,此代码无效。
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);
})
但是当我将socket Object更改为本地值时,它运行良好。
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();
});