2023.5.21第二阶段冲刺日报(七)

发布时间 2023-05-21 23:52:22作者: Arkiya

在网上查阅资料的过程中,还了解到可以基于webscoket进行视频会议

package com.webscoket;

import java.util.ArrayList;
import java.util.List;

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;
@ServerEndpoint("/test/{sid}")
public class WebSocketTest{
	//记录在线人数
	private static int onlineCount = 0;
	//记录在线人的相关信息
	private static List<WebSocketTest> webSocketSet = new ArrayList<WebSocketTest>();
	private Session session;
	//传个参数,区分一下发消息的每个人;
	private String sid=null;
	@OnOpen
	public void onOpen(Session session,@PathParam(value = "sid") String sid) {
		 this.session = session;
		 this.sid=sid;
         webSocketSet.add(this);     //加入set中
         addOnlineCount();
         System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
	}
	@OnClose
	public void onClose() {
		webSocketSet.remove(this);
		subOnlineCount();
		System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
	}
	@OnMessage
	public void onMessage(String message) {
		System.err.println(this.sid);
		if(this.sid.equals("001")) {
			WebSocketTest.sendMessageBySid(message, "002");
		}else {
			WebSocketTest.sendMessageBySid(message, "001");
		}
		
	}
	@OnError
	public void onError(Session session, Throwable error){
		System.err.println("发生错误");
		error.printStackTrace();
	}
	public static synchronized void addOnlineCount() {
		  WebSocketTest.onlineCount++;
    }
		 
	 public static synchronized void subOnlineCount() {
	     WebSocketTest.onlineCount--;
	 }
	 public static synchronized int getOnlineCount() {
		 return onlineCount;
     }
	 public static void sendMessage(String message) {
		//群发消息
			for(WebSocketTest item:webSocketSet) {
				try {
					item.session.getBasicRemote().sendText(message);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
	 }
	 public static void sendMessageBySid(String message,String sid) {
		 for(WebSocketTest item:webSocketSet) {
				try {
					if(item.sid.equals(sid)) {
						item.session.getBasicRemote().sendText(message);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
	 }
}

  

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
	var websocket = null;
	window.onload=function(){
		
		if('WebSocket' in window){
			websocket = new WebSocket("ws://localhost:8080/webscoketProject/test/001");
		}else{
			 alert('当前浏览器 Not support websocket')
		}
		websocket.onopen = function () {
			alert("webscoket连接成功!");
		}
		websocket.onmessage=function(event){
			$("#content").append("<div>user2:"+event.data+"</div>");
		}
	}
	function sendText(){
		var value=document.getElementById("client").value;
		$("#content").append("<div>user1:"+value+"</div>");
		websocket.send(value);
	}
	function leave(){
		websocket.close();
	}

	
</script>
</head>
<body>
	<input type="text" id="client"> 
	<button onclick="sendText()">发送</button>||<button onclick="leave()">关闭</button>
	<div id="content">
		
	</div>
</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
	var websocket = null;
	window.onload=function(){
		
		if('WebSocket' in window){
			websocket = new WebSocket("ws://localhost:8080/webscoketProject/test/002");
		}else{
			 alert('当前浏览器 Not support websocket')
		}
		websocket.onopen = function () {
			alert("webscoket连接成功!");
		}
		websocket.onmessage=function(event){
			$("#content").append("<div>user1:"+event.data+"</div>");
		}
	}
	function sendText(){
		var value=document.getElementById("server").value;
		$("#content").append("<div>user2:"+value+"</div>");
		websocket.send(value);
	}
	function leave(){
		websocket.close();
	}
</script>
</head>
<body>
	<input type="text" id="server"> 
	<button onclick="sendText()">发送</button>||<button onclick="leave()">关闭</button>
	<div id="content">
		
	</div>
</body>
</html>

  通过这种方法可以实现多人再文本界面的聊天,但是不能实现视频效果