Hi everyone,
Here is the Socket.IO Client library wrap for this Github Project.
Click here for the B4i wrapper
SocketIOClient
Author: @Biswajit
Version: 2.5
Dependency: OkHttpUtils2
Example Attached.
For the server code you can check Socket.IO Github example or you can use this following chat server example (written in javascript) [attached]:
To run this example server code:
Here is the Socket.IO Client library wrap for this Github Project.
Click here for the B4i wrapper
SocketIOClient
Author: @Biswajit
Version: 2.5
Dependency: OkHttpUtils2
- SocketIOClient
Method:- initialize(EventName As String)
- connect (host As String, params As String, secure As Boolean)
- connectWithOptions(host As String, params As String, secure As Boolean, reconnection As Boolean, reconnectionAttempts As Long, reconnectionDelay As Long, reconnectionDelayMax As Long, timeout As Long)
- disconnect()
- emit(event As String, data As Object)
- addEvent(event As String, callback As String)
- removeEvent(event As String, callback As String)
- sendAck(ack As Object, data() As Object)
- Events:
- OnConnecting: will be raised at the first time it tries to connect to the server
- OnConnectError(error As Object): will be raised if any error occurs when connecting
- OnConnectionTimeout(timeout As Object): will be raised if the connection does not receive a response from the server after approximately 30 seconds
- OnConnect: will be raised on successful connection
- OnDisconnect(reason As Object): will be raised on connection disconnect. If the disconnection was initiated by the server, you need to reconnect manually else it will try itself to reconnect
- OnReconnectAttempt(attemptNumber As Object): will be raised if it tries to reconnect after connection timeout or if connection disconnected from client side
- OnReconnecting(attemptNumber As Object): will be raised on reconnection attempt
- OnReconnect(attemptNumber As Object): will be raised on successful connection after connection timeout or if connection disconnected from client side
- OnReconnectError(error As Object): will be raised if any error occurs when reconnecting
- OnReconnectFailed: will be raised if when couldn’t reconnect within reconnectionAttempts
- OnError(error As Object): will be raised if any error occurs
Example Attached.
For the server code you can check Socket.IO Github example or you can use this following chat server example (written in javascript) [attached]:
PHP:
var express = require('express');
var app = express();
// *** FOR HTTPS CONNECTION *** //
// const fs = require('fs');
// var options = {
// key: fs.readFileSync('./key.pem'),
// cert: fs.readFileSync('./cert.pem'),
// passphrase: "password of cert/key file"
// };
// var server = require('https').createServer(options, app);
// *** FOR HTTPS CONNECTION *** //
// *** FOR HTTP CONNECTION *** //
var server = require('http').Server(app);
// *** FOR HTTP CONNECTION *** //
var io = require('socket.io')(server);
var users = {};
server.listen(999, function(){
console.log('listening on *:999');
});
io.sockets.on('connection', function(socket){
console.log("User Connected");
socket.on('user_msg', function(data,callback){
io.emit('new_message', data);
callback("Data Received");
});
socket.on('disconnect', function(data){
console.log('User disconnected');
});
});
To run this example server code:
- Download NodeJS.
- Install it.
- Download library zip file and extract.
- Run cmd inside server folder
- Run npm install
- It will take some time to complete.
- After that run node app.js
- It will show listening on *:999
- Now create you app and connect.
- That's it!
- If you want to use HTTPS (secure connection) then for testing you can generate key and cert file using openssl
Eg: openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
- Now you can declare this in process_globals
- Some event signature has been modified (please check above)
- New event added OnError
- Conflict with B4A OkHttpUtils2 library (Fixed)
- Added support for a secure connection.
- Now you can detect data delivery via an acknowledgment of the emit event (Check Attached Example).
- Send acknowledgment with data to the server on receiving a message.
- Added query parameter support.
- Send any object instead of a string.
Attachments
Last edited: