第14周博客
项目进展
1.更新前端
2.使用websocket技术实现服务器响应客户端
3.增加显示黄金点折线图
具体实现
后端websocket代码
package GoldenPoint.Player;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.json.JSONObject;
import org.json.JSONString;
@ServerEndpoint(value = "/websocket/{user_id}")
public class WebSocketServer {
//静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
private static int onlineCount = 0;
public static List <String> idArray =new ArrayList<String>();
public static List <String> numberArray=new ArrayList<String>();
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
private String user_id;
@OnOpen
public void onOPen(@PathParam("user_id") String user_id, Session session) {
this.user_id = user_id;
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
try {
sendMessage("{\""+user_id+"\":\"连接成功\"}");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnMessage
public void onMessage(String message, Session session) throws EncodeException, IOException {
// 取出发送内容,首先取出发送用户
System.out.println(message);
String xmlS = message;
String user_id="";
String user_number="";
String room_number="";
String host_flag="";
String times="";
//取出用户名
int user_id_s = xmlS.indexOf("user_id");
int user_id_e = xmlS.indexOf("/user_id");
user_id = xmlS.substring(user_id_s + 7, user_id_e);
//取出房间号
int room_number_s = xmlS.indexOf("room_number");
int room_number_e = xmlS.indexOf("/room_number");
if(room_number_s!=-1)
room_number = xmlS.substring(room_number_s + 11, room_number_e);
//取出轮数
int times_s = xmlS.indexOf("times");
int times_e = xmlS.indexOf("/times");
if(times_s!=-1)
times = xmlS.substring(times_s + 5, times_e);
//取出房主标志
int host_flag_s = xmlS.indexOf("host_flag");
int host_flag_e = xmlS.indexOf("/host_flag");
if(host_flag_s!=-1)
host_flag = xmlS.substring(host_flag_s + 9, host_flag_e);
//取出用户数字
int user_number_s = xmlS.indexOf("user_number");
int user_number_e = xmlS.indexOf("/user_number");
if(user_number_s!=-1) {
user_number = xmlS.substring(user_number_s + 11, user_number_e);
if(idArray.indexOf(user_id)==-1){
idArray.add(user_id);
numberArray.add(user_number);
}
}
//用户名不为空,显示用户名和当前人数
if(!user_id.equals("")) {
System.out.println("用户名:"+user_id);
System.out.println("当前在线人数!"+ onlineCount);
}
//数字不为空,显示用户名和数字
if(!user_number.equals("")) {
System.out.println("用户名:"+user_id+"--用户数字:"+user_number);
}
//房主标志为true,结算本轮游戏
if(host_flag.equals("true")) {
String ids="";
String nums="";
for(int i=0;i<idArray.size();i++) {
if(i!=idArray.size()-1){
ids+=idArray.get(i)+",";
nums+=numberArray.get(i)+",";
}else {
ids+=idArray.get(i);
nums+=numberArray.get(i);
}
}
System.out.println(ids);
System.out.println(nums);
String send_URL="http://localhost:8080/GoldenPoint_war_exploded/ServletPlayer?my_id="+ids.toString()+"&my_number="+nums.toString()+"×="
+times+"&room_number="+room_number;
BufferedReader br = null;
String source;
String resultStr="";
try {
URL url = new URL(send_URL);
URLConnection conn = url.openConnection();
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((source=br.readLine())!=null) {
resultStr+=source;
System.out.println(source);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//一轮结束,清空列表
idArray.clear();
numberArray.clear();
System.out.println(resultStr);
sendInfo(resultStr);
}
}
@OnClose
public void onClose() {
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}
@OnError
public void onErroe(Session session, Throwable error) {
System.out.println("发生错误");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 群发自定义消息
* */
public void sendInfo(String message) throws IOException {
for (WebSocketServer item : webSocketSet) {
item.sendMessage(message);
}
}
/**
* 定向自定义消息
* */
public void sendSomeoneInfo(String message,String someone) throws IOException {
for (WebSocketServer item : webSocketSet) {
try {
if(item.user_id.equals(someone)) {
item.sendMessage(message);
System.out.println("开始推送信息到:"+someone);
}
} catch (IOException e) {
continue;
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
效果展示
登录界面
用户注册界面
房间登录界面
房间注册
游戏界面
结算画面
积分榜
阶段总结
项目基本完成,功能可以正常使用。没有出现重大错误,后续考虑添加返回游戏人数和确认是否参加本轮,同时添加个人信息模块,查看在每一个房间的分数详情。