centrifugal / centrifuge-mobile

iOS and Android clients for Centrifugo and Centrifuge library using gomobile on top of centrifuge-go
MIT License
57 stars 14 forks source link

Final step: update mobile examples #4

Closed FZambia closed 7 years ago

FZambia commented 7 years ago

And then looks we are done

0xp3p3 commented 7 years ago

How to receive messages? it seems, there is not receiveMessageHandler like ios native sample. right? I can't get messages OnMessage handler.

FZambia commented 7 years ago

@TianDaGe here is an example for Go , for iOS, for Android

0xp3p3 commented 7 years ago

Hello @FZambia

I used config.json like this.

{
  "secret": "secret",
  "web": true,
  "admin_password": "password",
  "admin_secret": "secret",
  "publish": true,
  "presence": true,
  "join_leave": true,
  "watch": true,
  "namespaces": [
    {
      "name": "public",
      "anonymous": true,
      "publish": true,
      "watch": true,
      "presence": true,
      "join_leave": true,
      "history_size": 10,
      "history_lifetime": 30,
      "recover": true
    }  
  ]
}

Then i can receive message in localhost admin. but I can't get message in other side.(pc or mobile app) what am i wrong?

FZambia commented 7 years ago

Sorry but I can't say what's wrong seeing just a Centrifugo config file, most probably your connection with Centrifugo was not successfully established for some reason - wrong token, wrong connection address maybe?

0xp3p3 commented 7 years ago

pubish is working fine. Only onMessage is not working fine. I will share objective-c code.


#import <Centrifuge/Centrifuge.h>

#import "ViewController.h"

@interface ViewController ()<CentrifugeConnectHandler, CentrifugeDisconnectHandler, CentrifugeMessageHandler>
{
    CentrifugeSub *sub;
}

@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _label.text = @"Connecting...";

    CentrifugeCredentials *creds = CentrifugeNewCredentials(@"42", @"1488055494", @"", @"24d0aa4d7c679e45e151d268044723d07211c6a9465d0e35ee35303d13c5eeff");
    NSString *url = @"ws://192.168.1.109:9000/connection/websocket";

    CentrifugeClient *client = CentrifugeNew(url, creds, CentrifugeNewEventHandler(), CentrifugeDefaultConfig());
    NSError *error = nil;
    if (![client connect:&error]) {
        if (error != nil) {
            _label.text = @"Error on connect...";
        }
    }

    CentrifugeSubEventHandler *subEventHandler = CentrifugeNewSubEventHandler();
    [subEventHandler onMessage:self];

    error = nil;
    sub = [client subscribe:@"public:chat" events:subEventHandler error:&error];
    if (error != nil) {
        _label.text = @"Subscribe error";
    }

    NSString *data = @"{\"input\": \"hello\"}";
    error = nil;
    if (![sub publish:[data dataUsingEncoding:NSUTF8StringEncoding] error:&error]) {
        if (error != nil) {
            _label.text = @"Publish error";
        }
    }
}

- (void)onConnect:(CentrifugeClient *)p0 p1:(CentrifugeConnectContext *)p1 {
    _label.text = [NSString stringWithFormat:@"Connected %@", p1.clientID];
}

- (void)onDisconnect:(CentrifugeClient *)p0 p1:(CentrifugeDisconnectContext *)p1 {
    _label.text = [NSString stringWithFormat:@"Disconnected..."];
}

- (void)onMessage:(CentrifugeSub *)p0 p1:(CentrifugeMessage *)p1 {
    _label.text = [NSString stringWithFormat:@"%@, %@", p0.channel, p1.data];
}

- (IBAction)clickedTest:(id)sender {
    NSString *data = @"{\"input\": \"hello1234567\"}";
    NSError *error = nil;
    if (![sub publish:[data dataUsingEncoding:NSUTF8StringEncoding] error:&error]) {
        if (error != nil) {
            _label.text = @"Publish error";
        }
    }
}
FZambia commented 7 years ago

Do onConnect and onDisconnect callbacks work in your case?

0xp3p3 commented 7 years ago

I have just checked, it doesnt' work

FZambia commented 7 years ago

Then I think that most probably the problem is in how you register those callbacks. Of course this can be library problem but it worked for me when using Swift. I need some time to figure out how to properly register callbacks in Objective C

FZambia commented 7 years ago

Could you share code where you also registering connect and disconnect callbacks - it can help me

0xp3p3 commented 7 years ago

Sorry, Above code is full code.

FZambia commented 7 years ago

I never programmed in ObjC before but don't you need to write at least sth like this:

CentrifugeEventHandler *eventHandler = CentrifugeNewEventHandler();
[eventHandler onConnect:self];
[eventHandler onDisconnect:self];

CentrifugeClient *client = CentrifugeNew(url, creds, eventHandler, CentrifugeDefaultConfig());

To register connect and disconnect callbacks to event handler? I.e. the same as you do for subscription message handler

0xp3p3 commented 7 years ago

I think, this is correct for objective-c

@interface ViewController ()<CentrifugeEventHandler, ....>
... ... 
[eventHandler setDelegate:self]
... ...
- (void)onConnect:(CentrifugeClient *)p0 p1:(CentrifugeConnectContext *)p1 {
    _label.text = [NSString stringWithFormat:@"Connected %@", p1.clientID];
}

- (void)onDisconnect:(CentrifugeClient *)p0 p1:(CentrifugeDisconnectContext *)p1 {
    _label.text = [NSString stringWithFormat:@"Disconnected..."];
}
0xp3p3 commented 7 years ago

onMessage is also same... delegate must be registed into object... CentrifugeSubEventHandler *subEventHandler = CentrifugeNewSubEventHandler(); [subEventHandler setDelegate:self];

FZambia commented 7 years ago

Does it work now?

0xp3p3 commented 7 years ago

no, sir. there is not delegate in your current project.

FZambia commented 7 years ago

Hmm, I am almost sure that the problem here is just to find a way to properly register callbacks, looks like I need to dive a bit into ObjectiveC then to say how to do this using this bindings.

0xp3p3 commented 7 years ago

Currently onMessage function is calling one time in the first. because there is not delegate to get callback.

0xp3p3 commented 7 years ago

okay sir. Looking forward to hearing from you soon.

0xp3p3 commented 7 years ago

Hello, @FZambia Please check swift ios project on your actual device. Simulator is working fine, but actual device has one below issue.

clang: error: linker command failed with exit code 1 (use -v to see invocation)

FZambia commented 7 years ago

@TianDaGe thanks for pointing - I'll find a way to check it out, I tested on real Android device only yet.

FZambia commented 7 years ago

@TianDaGe just tried ObjectiveC in emulator - everything works, here is a code:

//
//  ViewController.m
//  CentrifugoObjectiveC
//
//  Created by Alexander Emelin on 05/04/2017.
//  Copyright © 2017 Alexander Emelin. All rights reserved.
//
#import "Centrifuge/Centrifuge.objc.h"
#import "ViewController.h"

@interface ViewController () <CentrifugeConnectHandler, CentrifugeDisconnectHandler, CentrifugeMessageHandler>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CentrifugeCredentials *creds = CentrifugeNewCredentials(@"42", @"1488055494", @"", @"24d0aa4d7c679e45e151d268044723d07211c6a9465d0e35ee35303d13c5eeff");
    NSString *url = @"ws://localhost:8000/connection/websocket";

    CentrifugeEventHandler *eventHandler = CentrifugeNewEventHandler();
    [eventHandler onConnect:self];
    [eventHandler onDisconnect:self];
    CentrifugeClient *client = CentrifugeNew(url, creds, eventHandler, CentrifugeDefaultConfig());
    NSError *error = nil;
    if (![client connect:&error]) {
        if (error != nil) {
            NSLog(@"Error connect");
        }
    }

    CentrifugeSubEventHandler *subEventHandler = CentrifugeNewSubEventHandler();
    [subEventHandler onMessage:self];

    error = nil;
    [client subscribe:@"public:chat" events:subEventHandler error:&error];
    if (error != nil) {
        NSLog(@"Subscribe error");
    }
}

- (void)onMessage:(CentrifugeSub *)p0 p1:(CentrifugeMessage *)p1 {
    NSLog(@"Messsage received");
}

- (void)onConnect:(CentrifugeClient *)p0 p1:(CentrifugeConnectContext *)p1 {
    NSLog(@"Connected");
}

- (void)onDisconnect:(CentrifugeClient *)p0 p1:(CentrifugeDisconnectContext *)p1 {
    NSLog(@"Disconnected");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

I launched app, then sent several messages into channel public:chat, then stopped Centrifugo. Here is an output in console:

2017-04-06 00:17:52.087 CentrifugoObjectiveC[29854:2009993] Connected
2017-04-06 00:18:08.551 CentrifugoObjectiveC[29854:2010049] Messsage received
2017-04-06 00:18:13.805 CentrifugoObjectiveC[29854:2010043] Messsage received
2017-04-06 00:18:14.304 CentrifugoObjectiveC[29854:2010049] Messsage received
2017-04-06 00:18:14.750 CentrifugoObjectiveC[29854:2010043] Messsage received
2017-04-06 00:18:21.970 CentrifugoObjectiveC[29854:2010055] Messsage received
2017-04-06 00:18:25.941 CentrifugoObjectiveC[29854:2010043] Messsage received
2017-04-06 00:20:40.881 CentrifugoObjectiveC[29854:2010043] Disconnected

I have no idea why it have not worked for you - maybe wrong header file? Here is screenshot to show you project structure (XCode Version 8.2.1):

screen shot 2017-04-06 at 00 23 00

I have not tested on real device yet, will write as soon as I do this.

FZambia commented 7 years ago

Successfully installed on real device (iPhone 5c), but I had to disable bitcode in project options:

screen shot 2017-04-06 at 01 42 35

because I came across a similar issue described here https://github.com/golang/go/issues/16966 - the fact that Go does not generate bitcode not a big problem at moment but it's hard to say what will be if Apple decides to do bitcode presence a requirement.

Actually there is a traceback in XCode (click on red exclamation mark after failed build) with more information about clang exception you got - hopefully it's the same.

0xp3p3 commented 7 years ago

You really awesome! Thanks. Real device is working fine, I think my XCode has some issues.

0xp3p3 commented 7 years ago

Hello @FZambia I got why the project is not working on simulator well. Did you get result on simulator or real device? I checked objective-c and swift project. If I include centrifuge.framework and run CentrifugeNewCredentials(), the simulator is crashing app with below error. Message from debugger: The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log. I have found about this error, but I can't get reason. perhaps is there one problem in the project? Anyway, real device is working fine. My XCode version is 8.3.

FZambia commented 7 years ago

At moment I've checked both simulator and real devices - both working fine for me. I will update my XCode to 8.3 and try again.

joeblew99 commented 7 years ago

@FZambia you have a 100% golang stack on server and mobile, so you can use golang quic instead of web sockets. Worth thinking from a code maintenance and perf aspect. QUIC is really easy to use and is not kernel dependent and runs on mobile networks MUCH bette than websockets. The telecom provides run everything using UPD (whihc is the transport QUIC ride on).

See this for just how easy it can be ! https://github.com/simia-tech/netx/blob/master/PRESENTATION.slide

FZambia commented 7 years ago

@joeblew99 thanks, I already thought about QUIC, maybe one day it will be in Centrifugo. We have little use on mobile at moment, so as soon as developers will integrate Centrifugo into mobile stack we can consider improvements in this area. Do you have experience with QUIC in mobile networks btw or maybe sth to read about this?

It's still important to have Websocket as stable and supported by browsers and native mobile language libraries transport.

0xp3p3 commented 7 years ago

Hello @FZambia How is it running on XCode 8.3?

FZambia commented 7 years ago

@TianDaGe already installed it but had no time to check - will do this later today

FZambia commented 7 years ago

@TianDaGe the same code I posted above works on iPhone 5c installed via XCode 8.3 too

0xp3p3 commented 7 years ago

Thanks for your reply, I will check on my side again.

FZambia commented 7 years ago

@TianDaGe any news, did you make it work for you?

0xp3p3 commented 7 years ago

no, @FZambia. Simulator debugging is crashed when app is started, but simulator works, not crash. So I am developing with actual device.

FZambia commented 7 years ago

Just updated Android and iOS examples to use latest bindings so we are done for a moment.

@TianDaGe that crash is strange, the error states for looking at crash log and filing a bug in XCode - maybe this is what you should do.