I have to make a chat for a Xamarin Forms (PCL) application. I'm using the NuGet package SocketIoClientDotNet for socket.
At first I could not connect at all. After many researches on internet I found this open issue on Github, so I downgraded the library but also all the dependencies:
- EngineIOClient.Net V0.9.22
- SocketIOClientDotNet V0.9.13
- WebSocket4Net V0.14.1.0
After that it was better, the connection seemed to work but I encountered a new issue: the connection is very instable and it's difficult for me to test anything cause of that. One time it can connect multiple times, one time it not connect at all, it's very annoying...
My code is very simple:
Common Code:
ISocketIO interface:
public interface ISocketIO
{
void Connect(string url);
void On(string eventString, Action<object> action);
}
MsgService class:
readonly string EVENT_CONNECT = "connect";
public MsgService(ISocketIO socket)
{
Socket = socket;
if (Socket != null)
{
Socket.On(EVENT_CONNECT, () =>
{
(code here...)
});
}
}
public void Connect()
{
if (Socket != null)
{
Socket.Connect("chat_url_here");
}
}
App class:
public partial class App : Application
{
public static MsgService MsgService;
public App(ISocketIO socket)
{
InitializeComponent();
Language = Language.FRENCH;
MsgService = new MsgService(socket);
MsgService.Connect();
MainPage = new NavigationPage(new MainPage());
}
...
}
iOS code (same for Android):
Class SocketIO
[assembly: Xamarin.Forms.Dependency(typeof(SocketIO))]
namespace MeetYou.iOS
{
public class SocketIO : ISocketIO
{
Socket _socket;
public void Connect(string url)
{
IO.Options opt = new IO.Options
{
Path = "path_here"
};
_socket = IO.Socket(url, opt);
_socket.Connect();
}
}
}
AppDelegate:
[Register("AppDelegate")]
public class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
{
Xamarin.Forms.Forms.Init();
LoadApplication(new App(new SocketIO()));
return base.FinishedLaunching(uiApplication, launchOptions);
}
}
Maybe I'm doing something wrong of maybe it exists an other plugin I could use instead this one.