Reconnect
The following is a simple demonstration that automatically reconnects the trading socket when a disconnection event is detected using a callback:
- Python
- Node.js
- C#
# A callback to receive event
def on_event(code, content):
print("===event=====")
print(code)
print(content)
if code == "300":
print("Reconnect")
try:
accounts = sdk.login("Your ID","Your password", "Your cert path", "Your cert password")
print("Reconnect successs")
except Exception as e:
print("Reconnect failed")
print(e)
print("========")
sdk.set_on_event(on_event)
sdk.setOnEvent(function(order, content) {
console.log("===Event===\n",order, content)
if(content[0] == "300"){
console.log("Reconnect")
try{
accounts = sdk.login("Your ID","Your password", "Your cert path", "Your cert password");
console.log("Reconnect success")
}catch(e){
console.log("Reconnect Failed")
}
}});
public class MyCallback : Callback
{
public string response = "";
private FubonSDK _sdk;
public MyCallback(ref FubonSDK sdk)
{
_sdk = sdk;
}
public void OnEvent(string code, string data)
{
Console.WriteLine(code);
Console.WriteLine(data);
if (code == "300")
{
Console.WriteLine("Reconnect");
try
{
_sdk.Login("Your ID","Your password", "Your cert path", "Your cert password");
Console.WriteLine("Reconnect success");
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine("Reconnect Failed");
}
}
}
}
var callback = new MyCallback(ref sdk);
sdk.RegisterCallback(callback);