Created
February 22, 2016 18:03
-
-
Save naderio/e402b3faed8d44ecdc77 to your computer and use it in GitHub Desktop.
socket.io implementation in NativeScript/Android based on socketio/socket.io-client-java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var observable = require("data/observable"); | |
const events = require('~/common/events'); | |
const params = require('~/common/params'); | |
const config = require('~/common/config'); | |
const erros = require('~/common/error'); | |
const json = require('~/common/utils/json'); | |
const debug = require('nativescript-debug')(__filename); | |
const _Emitter = io.socket.emitter.Emitter; | |
const _IO = io.socket.client.IO; | |
const _Socket = io.socket.client.Socket; | |
const _Ack = io.socket.client.Ack; | |
const Socket = (function (_super) { | |
__extends(Socket, _super); | |
function Socket(uri, opts) { | |
_super.call(this); | |
var _opts = new _IO.Options(); | |
Object.assign(_opts, opts); | |
_opts.query = 'token=' + config.token; | |
this._socket = _IO.socket(uri, _opts); | |
} | |
return Socket; | |
})(observable.Observable); | |
exports.Socket = Socket; | |
Socket.prototype.on = function (event, callback) { | |
// debug('on', arguments); | |
this._socket.on(event, new _Emitter.Listener({ | |
call: function (args) { | |
// debug('socket', event, arguments.length, args.length); | |
var payload = Array.prototype.slice.call(args); | |
// debug('socket', event, payload.length, payload); | |
var ack = payload.pop(); | |
// if (ack) { | |
// console.log(ack.getClass().getName(), !!ack.call); | |
// } | |
if (ack && !(ack.getClass().getName().indexOf('io.socket.client.Socket') === 0 && ack.call)) { | |
payload.push(ack); | |
ack = null; | |
} | |
payload = payload.map(json.deserialize); | |
debug('socket', event, payload); | |
if (ack) { | |
// debug('socket', event, 'ack'); | |
var _ack = ack; | |
// var ack = java.lang.reflect.Array.newInstance(io.socket.client.Ack.class.getField("TYPE").get(null)); | |
ack = function () { | |
// debug('socket', event, 'ack', arguments); | |
var args = Array.prototype.slice.call(arguments).map(json.serialize); | |
debug('socket', event, 'ack', args); | |
_ack.call(args); | |
}; | |
payload.push(ack); | |
} | |
// debug('socket', event, 'final', payload.length, payload); | |
callback.apply(null, payload); | |
}, | |
})); | |
}; | |
Socket.prototype.emit = function (event) { | |
// debug('emit', arguments); | |
var payload = Array.prototype.slice.call(arguments, 1); | |
var ack = payload.pop(); | |
if (ack && typeof ack !== 'function') { | |
payload.push(ack); | |
ack = null; | |
} | |
debug('emit', event, payload); | |
payload = payload.map(json.serialize); | |
if (ack) { | |
payload.push(new _Ack({ | |
call: function (args) { | |
args = Array.prototype.slice.call(args).map(json.deserialize); | |
debug('emit', event, 'ack', args); | |
ack.apply(null, args); | |
}, | |
})); | |
} | |
this._socket.emit(event, payload); | |
}; | |
Socket.prototype.connect = function () { | |
this.on('error', function (err) { | |
erros.report(new erros.QueryError(err, 'SocketError')); | |
}); | |
this.on('connect_error', function (err) { | |
erros.report(new erros.QueryError(err, 'SocketConnectError')); | |
}); | |
this.on('connect_timeout', function (err) { | |
erros.report(new erros.QueryError(err, 'SocketConnectTimeout')); | |
}); | |
// this.on('reconnect_error', function (err) { | |
// erros.report(new erros.QueryError(err, 'SocketReconnectError')); | |
// }); | |
this.on('reconnect_failed', function (err) { | |
erros.report(new erros.QueryError(err, 'SocketReconnectFailed')); | |
}); | |
this._socket.connect(); | |
}; | |
Socket.prototype.disconnect = function () { | |
this._socket.disconnect(); | |
}; | |
Object.defineProperty(Socket.prototype, 'connected', { | |
get: function () { | |
return this._socket.connected(); | |
} | |
}); | |
function _io(uri, opts) { | |
var socket = new Socket(uri, opts); | |
socket.connect(); | |
return socket; | |
} | |
_io.connect = _io; | |
module.exports = _io; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _io = require('~/lib/socket'); | |
var opts = { | |
query: 'token=' + config.token, | |
}; | |
var socket = _io(params.endpoint + '/v0', opts); | |
socket.on('connect', function () { | |
socket.emit('event', payload, function ack(result) { | |
// ... | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment