Package com.kyleduo.icomet.demo

Source Code of com.kyleduo.icomet.demo.Demo$NoneAuthChannelAllocator

package com.kyleduo.icomet.demo;

import com.kyleduo.icomet.Channel;
import com.kyleduo.icomet.ChannelAllocator;
import com.kyleduo.icomet.ICometCallback;
import com.kyleduo.icomet.ICometClient;
import com.kyleduo.icomet.ICometConf;
import com.kyleduo.icomet.IConnCallback;
import com.kyleduo.icomet.message.Message;
import com.kyleduo.icomet.message.Message.Content;

/**
* Demo
*
* @author keyleduo
*/
public class Demo {

  private static ICometClient mClient;

  public static void main(String args[]) {
    mClient = ICometClient.getInstance();
    ICometConf conf = new ICometConf();
    conf.host = "127.0.0.1";
    conf.port = "8100";
    conf.url = "stream";
    conf.iConnCallback = new MyConnCallback();
    conf.iCometCallback = new MyCometCallback();
    conf.channelAllocator = new NoneAuthChannelAllocator();
    mClient.prepare(conf);
    mClient.connect();
  }

  public static class MyConnCallback implements IConnCallback {

    @Override
    public void onFail(String msg) {
      System.out.println("connection fail");
      System.err.println(msg);
    }

    @Override
    public void onSuccess() {
      System.out.println("connection ok");
      mClient.comet();
    }

    @Override
    public void onDisconnect() {
      System.err.println("connection has been cut off");
    }

    @Override
    public void onStop() {
      System.out.println("client has been stopped");
    }

    @Override
    public boolean onReconnect(int times) {
      System.err.println("This is the " + times + "st times.");
      if (times >= 3) {
        return true;
      }
      return false;
    }

    @Override
    public void onReconnectSuccess(int times) {
      System.out.println("onReconnectSuccess at " + times + "st time");
      mClient.comet();
    }

  }

  public static class MyCometCallback implements ICometCallback {

    @Override
    public void onDataMsgArrived(Content content) {
      System.out.println("data msg arrived: " + content);
    }

    @Override
    public void onMsgArrived(Message msg) {
      System.out.println("msg arrived: " + msg);
    }

    @Override
    public void onErrorMsgArrived(Message msg) {
      System.err.println("error message arrived with type: " + msg.type);
    }

    @Override
    public void onMsgFormatError() {
      System.err.println("message format error");
    }

  }

  public static class NoneAuthChannelAllocator implements ChannelAllocator {

    @Override
    public Channel allocate() {
      Channel channel = new Channel();
      channel.cname = "kyle";
      channel.token = "token";
      channel.seq = 0;
      return channel;
    }
  }
}
TOP

Related Classes of com.kyleduo.icomet.demo.Demo$NoneAuthChannelAllocator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.