最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JGroups實現(xiàn)聊天小程序

本文實例為大家分享了JGroups實現(xiàn)聊天小程序的具體代碼,供大家參考,具體內容如下

成都創(chuàng)新互聯(lián)是專業(yè)的開封網(wǎng)站建設公司,開封接單;提供網(wǎng)站制作、網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行開封網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

效果圖:

JGroups實現(xiàn)聊天小程序

代碼部分:

package com.lei.jgoups;
 
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
 
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.util.Util;
 
public class SimpleChat extends ReceiverAdapter{
 JChannel channel;
 String user_name=System.getProperty("user.name", "n/a");
 final List state=new LinkedList();
 public static void main(String[] args) throws Exception {
 new SimpleChat().start();
 }
 private void start() throws Exception {
 channel=new JChannel();// 使用默認的配置, udp.xml【YBXIANG:】該文件位于jgroups-x.y.z.Final.jar中。
 channel.setReceiver(this);//注冊一個 Receiver 來接收消息并查看變化
 channel.connect("ChatCluster");
 channel.getState(null, 10000);
 eventLoop();
 channel.close();
 }
 
 private void eventLoop() {
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 while(true) {
 try {
 System.out.print(">"); 
 System.out.flush();
 String line=in.readLine().toLowerCase();
 if(line.startsWith("quit") || line.startsWith("exit"))
  break;
 line="[" + user_name + "] " + line;
 Message msg=new Message(null, line);
 channel.send(msg);
 }
 catch(Exception e) {
 }
 }
 }
 
 //如果有節(jié)點加入后會回調此函數(shù)
 public void viewAccepted(View new_view) {
 System.out.println("** view: " + new_view);
 }
 
 //接收到消息后會調用此函數(shù)
 public void receive(Message msg) {
 String line=msg.getSrc() + ": " + msg.getObject();
 System.out.println(line);
 synchronized(state) {//同步調用
 state.add(line);
 }
 }
 
 //getState回調方法
 public void getState(OutputStream output) throws Exception {
 synchronized(state) {
 Util.objectToStream(state, new DataOutputStream(output));
 }
 }
 
 // 從input stream中讀取狀態(tài),然后做相應的設置:
 public void setState(InputStream input) throws Exception {
 List list;
 list=(List)Util.objectFromStream(new DataInputStream(input));
 synchronized(state) {
 state.clear();
 state.addAll(list);
 }
 System.out.println(list.size() + " messages in chat history):");
 for(String str: list) {
 System.out.println(str);
 }
 }
}

架包:

JGroups實現(xiàn)聊天小程序

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


分享標題:JGroups實現(xiàn)聊天小程序
網(wǎng)站URL:http://fisionsoft.com.cn/article/pdpgps.html