我目前正在爲android開發一款遊戲。這場比賽有沉重的網絡。好的android網絡練習
有兩個獨立的線程比較好,一個用於接收消息,另一個用於發送消息。還是有一個單線程發送和接收消息更好?
public static void init(String h){
host=h;
connected=false;
instance = new Client();
new Thread(new Runnable() {
@Override
public void run() {
group = new NioEventLoopGroup();
try{
bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(instance);
channel = bootstrap.connect(host,PORT).sync().channel();
connected=true;
} catch (Exception e) {
if (notifier!=null){
notifier.onServerNoLongerReachable();
}
e.printStackTrace();
}
}
}).start();
}
public static void setNotifier(ClientInterface notif){
notifier = notif;
}
我使用了Netty,我有我的活動實現
public interface ClientInterface{
void onReceive(String msg);
void onServerNoLongerReachable();
}
一個線程將是理想,但是你目前遇到什麼問題來提示這個問題?你使用的是'RxJava',因爲這將有助於網絡請求。 –
我目前沒有遇到任何問題,我只是驗證我不會以錯誤的方式解決問題。 – logan20
你可以提供任何代碼作爲例子嗎? –