Skip to content

Instantly share code, notes, and snippets.

@btspoony
Created February 28, 2012 21:46
Show Gist options
  • Save btspoony/1935356 to your computer and use it in GitHub Desktop.
Save btspoony/1935356 to your computer and use it in GitHub Desktop.
My SocketIO Client Reciever for Flash
/**
* RealTime Server Connection
*/
MainGame.prototype.ioConnect = function() {
if (this.sio) return this.sio;
// Check if io exists
if ( isNULL(io) ) {
console.log('[Error] IO Lib Missing. Please Connect with Website Admin');
return null;
}
var self = this, sio;
server_url = settings['server_url'];
this.sio = sio = io.connect(server_url + 'sns')
.on('connect', function () {
console.log('IO Server Connected !');
// set common info
self.sio = sio;
self.msgQuery = [];
// on server global message
sio.on('common', function (msg) {
self.onCommonRecieve(msg);
})
.on('disconnect', function (msg) {
console.log('Server Disconnected!');
});
})
.on('error', function(msg) {
// Set global error handler
console.log('[Error] Connecting Server Error: ', msg);
//TODO Record IO setup error
self.msgQuery = null;
self.sio = null;
});
return sio;
};
/**
* RealTime Server Force Disconnect
*/
MainGame.prototype.ioDisconnect = function() {
if (!this.sio) return;
this.sio.disconnect();
this.msgQuery = null;
this.sio = null;
};
// Flash Bridge
MainGame.prototype.connectSWFBridge = function(window) {
var self = this;
// Call From Flash
window.emitRequest = function emitRequest() {
return self.emitRequest.apply(self, arguments);
};
// Call to Flash
window.sendToFlash = function sendToFlash() {
self.sendToFlash.apply(self, arguments);
};
};
/* ==!===== Communicating with Flash =========
* =========================================== */
// Send to Flash
MainGame.prototype.sendToFlash = function(from, err, data) {
if (err) console.log('error: ', err);
else console.log('Data from: '+ from + '; data:', data);
// If msg is in the query
var index = jQuery.inArray(from, this.msgQuery);
if (index >= 0) {
this.msgQuery.splice(index, 1);
this.swf.onResponse(from, err, data);
}else {
this.swf.serverMsg(from, err, data);
}
};
/**
* Emit to Server
*/
MainGame.prototype.emitRequest = function(target) {
var sio = this.sio, i, _args = [];
if (!sio || isNULL(target) ) return false;
// put all arguments into args
for (i = 1; i < arguments.length; i++) {
_args.push(arguments[i]);
}
// Send to server
var uid = geneUID(),
self = this;
self.msgQuery.push(uid);
sio.emit(target, { args: _args, reqid: uid }, function (err, data) { // Server Callback
// Check pass to Flash function
if (!self.sendToFlash) {
console.log('! No function self.sendToFlash');
return;
}
// if err
if (err) {
self.sendToFlash(uid, err, null);
}
// check data type
if ( !isNULL(data) ) {
self.sendToFlash(uid, null, data);
}
});
return uid;
};
/**
* On server global message
*/
MainGame.prototype.onCommonRecieve = function(msg) {
var err, from, data;
//check type
if (typeof msg === 'string') {
err = null,
from = 'sysmsg',
data = [msg];
}else if (typeof msg === 'object') {
err = msg.err,
from = msg.from,
data = msg.data;
// Check if all data correct
if (!from || !(data || err) ) {
console.log('Data incorrect');
return;
}
}else {
console.log('Message incorrect');
return;
}
self.sendToFlash(from, err, data);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment