package cn.jsxwsl.chat.client;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Net extends ChatRoomUI{
private static final long serialVersionUID = 1L;
private Socket socket = null;
private User bindUser = null;
private List<User> roomUserList = new LinkedList<>();
private Vector<String> chatOnlineUserNames = new Vector<>(); // 在线用户列表数据
public Net() {
setNet(this);
setVisible(true);
}
public User getBindUser() {
return bindUser;
}
public void connect(String host, String port) {
try {
String conUrl = "http://HOST:PORT".replace("HOST", host).replace("PORT", port);
System.out.println("connect:" + conUrl);
socket = IO.socket(conUrl);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("CONNECT");
}
});
socket.connect();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void startNetListner() {
this.getSocket().on("loginResult", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
JSONObject jsonRet = JSONObject.fromObject(Base64Util.getFromBase64((String) args[0]));
System.out.println("loginResult");
System.out.println(jsonRet);
if (jsonRet.get("errcode").toString().equals("0")) {
setLoginWindow(false);
setChatWindow(true);
int userId = jsonRet.getInt("userId");
JSONObject chatRoom = JSONObject.fromObject(jsonRet.get("chatRoom"));
System.out.println(chatRoom);
JSONArray userList = JSONArray.fromObject(chatRoom.get("userList"));
for (int i = 0; i < userList.size(); i++) {
JSONObject userJson = (JSONObject) userList.get(i);
User u = new User();
u.setId(userJson.getInt("userId"));
u.setNickname(userJson.getString("nickname"));
roomUserList.add(u);
chatOnlineUserNames.add(userJson.getString("nickname"));
if (u.getId() == userId) {
bindUser = u;
}
}
updateOnlineNames(chatOnlineUserNames);
// 还原聊天记录
JSONArray messageList = JSONArray.fromObject(chatRoom.get("messageList"));
for (int i = 0; i < messageList.size(); i++) {
JSONObject messageJson = (JSONObject) messageList.get(i);
String nickname = messageJson.getString("nickname");
String text = messageJson.getString("text");
int type = messageJson.getInt("type");
long time = messageJson.getLong("time");
createChatComponent(nickname, text, time*1000, type);
}
} else {
alert("登录失败:" + jsonRet.get("errmsg"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.getSocket().on("userEnterPush", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
JSONObject jsonRet = JSONObject.fromObject(Base64Util.getFromBase64((String) args[0]));
System.out.println("userEnterPush");
System.out.println(jsonRet);
JSONObject userJson = JSONObject.fromObject(jsonRet.get("user"));
User u = new User();
u.setId(userJson.getInt("userId"));
u.setNickname(userJson.getString("nickname"));
roomUserList.add(u);
chatOnlineUserNames.add(userJson.getString("nickname"));
updateOnlineNames(chatOnlineUserNames);
createChatComponent(u.getNickname(), u.getNickname()+"加入聊天室", new Date().getTime(), 2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.getSocket().on("newMessagePush", new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
System.out.println("newMessagePush");
JSONObject jsonRet = JSONObject.fromObject(Base64Util.getFromBase64((String) args[0]));
System.out.println(jsonRet);
String nickname = jsonRet.getString("nickname");
String text = jsonRet.getString("text");
int type = jsonRet.getInt("type");
long time = jsonRet.getLong("time");
createChatComponent(nickname, text, time*1000, type);
} catch (Exception e) {
e.printStackTrace();
}
}
});
this.getSocket().on("userQuitPush", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("userQuitPush");
JSONObject jsonRet = JSONObject.fromObject(Base64Util.getFromBase64((String) args[0]));
System.out.println(jsonRet);
int userId = jsonRet.getInt("userId");
chatOnlineUserNames = new Vector<>();
for (int i = 0; i < roomUserList.size(); i++) {
User user = roomUserList.get(i);
if (user.getId() == userId) {
roomUserList.remove(i);
createChatComponent(user.getNickname(), user.getNickname()+"退出聊天室", new Date().getTime(), 2);
continue;
}
chatOnlineUserNames.add(user.getNickname());
}
updateOnlineNames(chatOnlineUserNames);
}
});
this.getSocket().on("getOutOfLine", new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("getOutOfLine");
}
});
this.getSocket().on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("DISCONNECT");
bindUser = null;
roomUserList = new LinkedList<>();
chatOnlineUserNames = new Vector<>(); // 在线用户列表数据
closeConneting();
}
});
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public static void main(String[] args) {
new Net();
}
}
最近下载更多
qazzaq123 LV20
2021年2月8日
jaden12 LV3
2019年10月6日
black-shuai LV6
2019年5月23日
略略略1555 LV1
2019年4月22日
你好看 LV1
2019年1月7日
xiaoxiongmao LV2
2018年11月20日
shaogongzi LV1
2018年10月10日
jarlove LV7
2018年8月2日
我怕怕 LV3
2018年7月12日
Peter_ LV6
2018年7月4日
最近浏览更多
微信网友_7550620088864768 LV1
6月16日
chenranr LV10
2024年6月15日
微信网友_6526583469690880
2023年6月19日
暂无贡献等级
belike
2023年5月26日
暂无贡献等级
wzy5432 LV1
2023年4月18日
win1991 LV6
2023年3月27日
hhdbzz LV1
2022年12月29日
13133117021 LV5
2022年12月24日
wwfl02 LV3
2022年12月16日
12cq345 LV6
2022年10月13日

