Problem in receiving Registatation response - iOS Swift

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Problem in receiving Registatation response - iOS Swift

Postby Reddy » 18 Mar 2017, 05:57

Hi,

I am new to SmartFox server APIs. I am working on building iOS framework which uses SFS2X Client. When I am working with Login its perfectly fine that I am getting the response in "onLogin" method, but when when I am trying to register using user E-mail and getting response in synchronous way but either onLogin/onLoginError not being called.

I am not sure, how the server responses should be handled in iOS, it's more clear in Android examples but I can't follow the same. Please help me with the following.

1. How server response should be handled, single piece of code should handle it or I should have specific delegate method for each ? Please explain.
2. Documentation for different delegate methods for server responses.
3. Help me in point out any examples (I have already gone through examples which are part of SmartFox server documentation)

I am using the following for building my framework.

SFS2X API version - 1.6.1
Swift Version - 3.0
XCode Version - 8.2.1
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 18 Mar 2017, 09:35

Hello
Reddy wrote:I am new to SmartFox server APIs. I am working on building iOS framework which uses SFS2X Client. When I am working with Login its perfectly fine that I am getting the response in "onLogin" method, but when when I am trying to register using user E-mail and getting response in synchronous way but either onLogin/onLoginError not being called.

There is no "synchronous way".
You always send a request and wait for the response in an event handler, therefore asynchronously.

I am not sure, how the server responses should be handled in iOS, it's more clear in Android examples but I can't follow the same. Please help me with the following.

You just said that it works by sending the login and getting the response in the "onLogin" method... that is how it works :)

1. How server response should be handled, single piece of code should handle it or I should have specific delegate method for each ? Please explain.

Yes.

2. Documentation for different delegate methods for server responses.

You can find all delegate methods descriptions in the documentation.
http://docs2x.smartfoxserver.com/api-docs/objc-doc/

3. Help me in point out any examples (I have already gone through examples which are part of SmartFox server documentation)


Here's a basic example about connection and login:

Code: Select all

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ISFSEvents {

    var window: UIWindow?
    var sfs:SmartFox2XClient?
   
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        sfs = SmartFox2XClient(smartFoxWithDebugMode: true, delegate: self)
        NSLog("Version: %@", (sfs?.version)!)

        sfs?.connect("127.0.0.1", port: 9933)
       
        return true
    }
   
   
    // -------------------------------------------------------------------------------------------
    // SFS2X Event handlers
    // -------------------------------------------------------------------------------------------
   
    func onConnection(evt: SFSEvent!)
    {
       
        let data:NSDictionary = evt.params as NSDictionary
        let success:Bool = data.objectForKey("success") as! Bool
       
        if (success)
        {
            NSLog("Connection OK")
           
            // Send login
            sfs!.send(LoginRequest(userName: "", password: "", zoneName: "BasicExamples", params: nil))
        }
           
        else
        {
            NSLog("Connection failed")
        }
       
    }
   
    func onLogin(evt: SFSEvent!)
    {
        NSLog("Logged in as: %@", (sfs?.mySelf.name())!)
    }
   
    func onLoginError(evt: SFSEvent!)
    {
        let params:NSDictionary = evt.params as NSDictionary

        let errMess:String = params.objectForKey("errorMessage") as! String
        NSLog("Login ERROR:", errMess)
    }
    ...
    ...
    ...
}


All other events work similarly and provide different parameters which you can find out in the docs.
Also take a look at the Example pack here:

The examples are written in Objective-C, if you have a basic understanding of the language you can translate to swift very easily.
http://smartfoxserver.com/download/sfs2x#p=examples
Lapo
--
gotoAndPlay()
...addicted to flash games
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 21 Mar 2017, 11:08

Thanks a lot for your quick response.

when an existing user is trying to signup, either OnLogin or OnLoginError are NOT being called. Still I could see Client SDK console log as follows.

Success case console log:

2017-03-21 17:36:12.898 LoginTest[4677:2214782] [SFS - INFO]Handling Message: Login: { Message id: 1 }
{Dump: }

(short) ec: 201
(utf_string_array) ep: [(
"someemail.id@gmail.com"
)]

Error Case console log:

2017-03-21 16:31:41.645 LoginTest[4642:2203250] [SFS - INFO]Handling Message: Login: { Message id: 1 }
{Dump: }

(short) ec: 200
(utf_string_array) ep: [(
"someemail.id@gmail.com"
)]

I observed that Android example uses Broadcast receiver. In similar way, do I need to use NSNotificationCenter to handle the events in order to solve this problem ?

I am just exploring different ways to find out why I am not getting control to OnLoginError method.

I am not able to debug this issue further. Could you please suggest any approach to solve this issue?
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 21 Mar 2017, 14:26

Reddy wrote:Thanks a lot for your quick response.

when an existing user is trying to signup, either OnLogin or OnLoginError are NOT being called. Still I could see Client SDK console log as follows.

either OnLogin or OnLoginError ... well this is correct. Only one of the two will fire. If the login is successful you will get the LOGIN event, otherwise the LOGIN_ERROR will fire. It is expected to be an either/or for those two specific events.

I observed that Android example uses Broadcast receiver. In similar way, do I need to use NSNotificationCenter to handle the events in order to solve this problem ?

I have no idea what you're referring to. (Broadcast recevier? NSNotificationCenter?)

It would greatly help you could describe every step you're doing, what platform you're working on.
For instance you said that "an existing user is trying to signup" but you didn't explain how this is done? Is it handled by your custom server code? Or via the Signup Assistant we provide?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 27 Mar 2017, 07:19

We have custom code at server end for login and registration and which is working fine for Android client.

The following code is being used to connect, login and register the iOS client.

Code - Connecting server

smartFoxNew = SmartFox2XClient(smartFoxWithDebugMode:true, delegate: self)
smartFoxNew.logger.loggingLevel = LogLevel_DEBUG
let cfg = ConfigData()
cfg.host = “xx-yyy-zz-abc.someapp.net"
cfg.port = 9933
cfg.setZone("Registration")
cfg.useBlueBox = false
smartFoxNew.connect(withConfig: cfg)
smartFoxNew.connect()

Code - For Login

isfEvt.putInt("rqstType", value: 2) // 2 —> LOGIN_REQUEST”
isfEvt.putUtfString("device_id", value: "9774d56d682e549c")

smartFoxNew.send(LoginRequest .request(withUserName: userName, password:password, zoneName: "Registration", params: isfEvt) as! LoginRequest!)

Code - Login Response in case of Success

public func onLogin(_ evt: SFSEvent!) {
Handling the response
}

Code - Login Response in case of error
public func onLoginError(_ evt: SFSEvent!) {
Handling the response
}

Registration Code

isfEvt.putInt("rqstType", value: 1) // 1 —> SIGN_UP_REQUEST
isfEvt.putUtfString("device_id", value: "9774d56d682e549c")

smartFoxNew.send(LoginRequest .request(withUserName: userName, password:password, zoneName: "", params: isfEvt) as! LoginRequest!)

In case of registration, we are NOT able to get callback to either onLogin or OnLoginError method and this is happening in both registration success and failure as well. However, we are able to see the result in the Xcode console in case of registration success and failure (probably it is from SmartFox Client SDK)

We have a query here. Do we need to register for a specific event in order to get the response in our code? We have seen this code in Android side like, “sfsClient.addEventListener(SFSEvent.LOGIN_ERROR, this)” where as we are not able to find similar code in any iOS code examples.

Below is the console response that we see in our Xcode, in case of getting a response from SmartFox server.

(short) ec: 201
(utf_string_array) ep: [(
"someemail.id@gmail.com"
)]


Thank you for the support and we appreciate your help.
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 01 Apr 2017, 13:03

Any Update on this ?

I would really appreciate if you can help us in solving this problem, thank you.
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 03 Apr 2017, 07:28

I am posting this again as we are in a critical situation and our project deadlines are getting effected because of these issues. We have already purchased a life time licence of SmartFox server and building a product on both Android and iOS. However, there are some basic things are not working when it comes to iOS, And we need your help in solving this problem. Please let us know if you need any more information to understand this problem.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 03 Apr 2017, 09:00

The first problem I see with your code is that you're using a LoginRequest to sign up a user.
This is doesn't sound like a good idea, because you're using a totally unrelated request to accomplish something different.

Instead the proper way to sign up users would be as follows:

1-> Create a specific Zone only for new users that need to create an account.
2-> When a user decides to sign up you connect and login to the "Signup" zone, with a guest login. There the user can talk to your Extension and perform all the necessary steps to create the new account
3 -> Finally you can logout the user from the Signup Zone and login into your application Zone.

As I previously mentioned we provide server-side components (Signup Assistant and Login Assistant) to simplify this process, with detailed documentation and tutorials.

Of course, you can still do it your way without using the assistants, if you so desire. In which case I recommend to use the workflow I just explained.

We have a query here. Do we need to register for a specific event in order to get the response in our code?

If you by code you mean the client side then... yes, of course.
If you want to send a certain request to the server (e.g. LoginRequest) you must register for its client events (LOGIN and LOGIN_ERROR) otherwise you won't get any replies.

In Objective-C / Swift it works slightly differently than Java as you need to implement the ISFSEvents protocol in your main class and pass that as the the delegate. The protocol supports all event callbacks. I recommend taking a closer look at the Objective-C documentation:
http://docs2x.smartfoxserver.com/api-docs/objc-doc/

For example:

Code: Select all

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ISFSEvents {

    var window: UIWindow?
    var sfs:SmartFox2XClient?
   
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        sfs = SmartFox2XClient(smartFoxWithDebugMode: true, delegate: self)
        NSLog("Version: %@", (sfs?.version)!)

        sfs?.connect("127.0.0.1", port: 9933)
        return true
    }
   
   
    // -------------------------------------------------------------------------------------------
    // SFS2X Event handlers
    // -------------------------------------------------------------------------------------------
   
    func onConnection(evt: SFSEvent!)
    {
       
        let data:NSDictionary = evt.params as NSDictionary
        let success:Bool = data.objectForKey("success") as! Bool
       
        if (success)
        {
            NSLog("Connection OK")
           
            // Send login
            sfs!.send(LoginRequest(userName: "", password: "", zoneName: "BasicExamples", params: nil))
        }
           
        else
        {
            NSLog("Connection failed")
        }
       
    }
   
    func onLogin(evt: SFSEvent!)
    {
        NSLog("Logged in as: %@", (sfs?.mySelf.name())!)
    }
   
    func onLoginError(evt: SFSEvent!)
    {
        let params:NSDictionary = evt.params as NSDictionary

        let errMess:String = params.objectForKey("errorMessage") as! String
        NSLog("Login ERROR:", errMess)
    }


Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 05 Apr 2017, 06:29

Thank you for the response.

The first problem I see with your code is that you're using a LoginRequest to sign up a user.
This is doesn't sound like a good idea, because you're using a totally unrelated request to accomplish something different.


Even we tried with Signup objective C code provided by SmartFox and it uses ExtensionRequest. However, Application is NOT able to get the callback on -(void) onExtensionResponse:(SFSEvent *)evt method, and this was tested with both our own custom server and also basic sever code provided by SmartFox running in the localhost. Please let us know how it should work with the sample SignUp code that is provided by you in iOS, especially in case of callback mechanism.

SmartFox code link : http://docs2x.smartfoxserver.com/ExamplesIOS/signup

Instead the proper way to sign up users would be as follows:

1-> Create a specific Zone only for new users that need to create an account.
2-> When a user decides to sign up you connect and login to the "Signup" zone, with a guest login. There the user can talk to your Extension and perform all the necessary steps to create the new account
3 -> Finally you can logout the user from the Signup Zone and login into your application Zone.

As I previously mentioned we provide server-side components (Signup Assistant and Login Assistant) to simplify this process, with detailed documentation and tutorials.

Of course, you can still do it your way without using the assistants, if you so desire. In which case I recommend to use the workflow I just explained.


Also our server code uses a different zone called "Registration" zone for the registration and profile operations, it's working well with our existing Android product.

In Objective-C / Swift it works slightly differently than Java as you need to implement the ISFSEvents protocol in your main class and pass that as the the delegate. The protocol supports all event callbacks. I recommend taking a closer look at the Objective-C documentation:


Our Swift code is implementing ISFSEvents protocal as it was explained by you. We might be missing something at our end, either at Client or server end.

Please help in make sample SignUp code that is provided by SmartFox. So, we can take it from there.
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 05 Apr 2017, 07:36

I would highly recommend to start from our Example in Objective-C which does work out of the box.

If you're having issues with an Extension call that doesn't return a reply please check your server logs, it's very likely that there's an error on the server side.
Also make sure that your Extension is really started up when the server boots. If there's any problem with starting your Extension you will find errors in the log files.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 06 Apr 2017, 13:46

Thanks for helping us.

As you recommended, we tried with Example Objective-C code with the local server and we had some errors at the time of registration. Please see the attached server log screenshot https://lh3.googleusercontent.com/-L82l2PEf4QM/WOYtOhwAXqI/AAAAAAAAAGA/6cnVp7iL8rQvaaaKFh3xKAGO1wjUBemIQCL0B/h667/2017-04-06.png

On the other note, Our remote server version is 2.09 where as I am using the iOS client SDK of 2.12. Would that cause some issues like Callback handlers are not working ?

I am working on resolving the local serve issues, will update you once they are resolved.

Thanks
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 06 Apr 2017, 13:54

It doesn't seem to me that this is an issue of versions, rather of setup.
The error you have shown indicates that the client has called a Room Extension (Room id = 1) but the server is complaining that the Room does not have an Extension attached.

In our Signup Examples we don't use Room Extension we Zone Extension, so I think you're doing something wrong. Please double check your code...

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Reddy
Posts: 8
Joined: 18 Mar 2017, 04:37

Re: Problem in receiving Registatation response - iOS Swift

Postby Reddy » 07 Apr 2017, 08:45

OK, I got it. We will try with zone and will look at the server set up.

On the other note, we quickly tried using Simplechat Objective C application with the local server and got the following error log on login request at the application end. Are we missing anything ? Can you please help us?


2017-04-07 12:11:24.077 SimpleChat[1380:59976] [SFS - INFO]SFS2X API version: 1.6.1
2017-04-07 12:11:24.165233 SimpleChat[1380:60060] [] ____nwlog_simulate_crash_inner_block_invoke dlopen CrashReporterSupport failed
2017-04-07 12:11:24.165628 SimpleChat[1380:60060] [] __nwlog_err_simulate_crash simulate crash failed "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"
2017-04-07 12:11:24.167026 SimpleChat[1380:60060] [] nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available, dumping backtrace:
[i386] libnetcore-856.30.16
0 libsystem_network.dylib 0x039c314d __nw_create_backtrace_string + 123
1 libnetwork.dylib 0x045db7a6 nw_socket_add_input_handler + 3588
2 libnetwork.dylib 0x045b66fe nw_endpoint_flow_attach_protocols + 4199
3 libnetwork.dylib 0x045b557a nw_endpoint_flow_setup_socket + 581
4 libnetwork.dylib 0x045b41ed -[NWConcrete_nw_endpoint_flow startWithHandler:] + 2545
5 libnetwork.dylib 0x045d1cf0 nw_endpoint_handler_path_change + 2835
6 libnetwork.dylib 0x045d10f6 nw_endpoint_handler_start + 589
7 libnetwork.dylib 0x045f0dc6 __nw_connection_start_block_invoke + 454
8 libdispatch.dylib 0x03761396 _dispatch_call_block_and_release + 15
9 libdispatch.dylib 0x0378ccc3 _dispatch_client_callout + 14
10 libdispatch.dylib 0x037693ae _dispatch_queue_serial_drain + 1619
11 libdispatch.dylib 0x03769bc2 _dispatch_queue_invoke + 1109
12 libdispatch.dylib 0x0376a001 _dispatch_queue_override_invoke + 497
13 libdispatch.dylib 0x0376c5e4 _dispatch_root_queue_drain + 470
14 libdispatch.dylib 0x0376c3a6 _dispatch_worker_thread3 + 143
15 libsystem_pthread.dylib 0x03bdd25c _pthread_wqthread + 1050
16 libsystem_pthread.dylib 0x03bdaf56 start_wqthread + 34
2017-04-07 12:11:24.225 SimpleChat[1380:59976] [SFS - INFO]sending handshake request: isReconnection : NO
2017-04-07 12:11:24.226 SimpleChat[1380:59976] [SFS - INFO]WRITE - Creating a new buffer
2017-04-07 12:11:24.227 SimpleChat[1380:59976] [SFS - INFO]Data Write: Binary Size: 60
80 00 39 12 00 03 00 01 63 02 00 00 01 61 03 00 ..9.....c....a..
00 00 01 70 12 00 02 00 03 61 70 69 08 00 05 31 ...p.....api...1
2e 36 2e 31 00 02 63 6c 08 00 11 49 4f 53 20 76 .6.1..cl...IOS.v
65 72 73 69 6f 6e 3a 20 31 30 2e 32 ersion:.10.2
2017-04-07 12:11:24.229 SimpleChat[1380:59976] [SFS - INFO]WRITE - Written directly to outStream len:60
2017-04-07 12:11:24.238 SimpleChat[1380:59976] [SFS - INFO]Data Read: Binary Size: 80
80 00 4d 12 00 03 00 01 70 12 00 03 00 02 63 74 ..M.....p.....ct
04 00 00 04 00 00 02 6d 73 04 00 07 a1 20 00 02 .......ms.......
74 6b 08 00 20 30 61 64 35 61 34 35 37 63 34 63 tk...0ad5a457c4c
66 33 33 36 33 63 65 65 35 63 36 62 66 66 66 65 f3363cee5c6bfffe
63 36 63 38 32 00 01 61 03 00 00 00 01 63 02 00 c6c82..a.....c..
2017-04-07 12:11:24.239 SimpleChat[1380:59976] [SFS - INFO]Handling Message: Handshake: { Message id: 0 }
{Dump: }

(utf_string) tk: 0ad5a457c4cf3363cee5c6bfffec6c82
(int) ms: 500000
(int) ct: 1024
2017-04-07 12:11:24.240 SimpleChat[1380:59976] AppDelegate::onConnection
2017-04-07 12:11:28.786261 SimpleChat[1380:59976] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/alswamy/Library/Developer/CoreSimulator/Devices/AE1B90BF-51B6-43F1-A353-71FB37FE70E5/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-04-07 12:11:28.786758 SimpleChat[1380:59976] [MC] Reading from private effective user settings.
2017-04-07 12:11:57.250 SimpleChat[1380:59976] [SFS - INFO]Data Read: Binary Size: 0
2017-04-07 12:11:57.250 SimpleChat[1380:59976] [SFS - WARN]Exception onDataRead!!!
2017-04-07 12:13:04.686 SimpleChat[1380:59976] [SFS - WARN]Not connected: can't send any request: <LoginRequest: 0x7c978f00>
User avatar
Lapo
Site Admin
Posts: 23009
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem in receiving Registatation response - iOS Swift

Postby Lapo » 07 Apr 2017, 09:51

Hi,
the error you have reported is related to XCode 8 only. It's an issue only for the Simulator and you can make it go away following this article:
http://stackoverflow.com/questions/3954 ... -backtrace

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 53 guests