描述情况:
网站:g2.16861.com:2303/WebTest/Video.html
打开两个上面的这个网址 然后等待一下缓存 任意一个点击播放 另一个会同步播放
同步数据是正常的 我监听事件可能有大问题
问题如下:
当任意一个点击拖动进度条时 这时候BUG就出现了 疯狂向后台发送play 和 stop 指令
一开始我想用事件监听seek完成 但是发现我完不成 只能求助各位大佬指点一下了!
/**
*
*/
客户端js
myVideo.addEventListener('pause', synchronization);
myVideo.addEventListener('play',synchronizationplay);
myVideo.addEventListener('seeked',synchronizationchange);
function synchronizationchange(){
myVideo.pause();
}
function synchronization(){
send('stop');
}
function synchronizationplay(){
send('play');
}
var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('WebSocket' in window) {
websocket = new WebSocket("ws://192.168.43.22:8080/WebTest/websocket");//注意此处的ws要加上项目名JavaWebSocket再加@ServerEndpoint注解的值websocket
}
else {
alert('当前浏览器 Not support websocket');
}
function myFunction(){
var video=myVideo;
var time='时间:'+video.currentTime;
myVideo.pause();
send(time);
}
//连接发生错误的回调方法
websocket.onerror = function () {
setMessageInnerHTML("WebSocket连接发生错误");
}
//连接成功建立的回调方法
websocket.onopen = function () {
setMessageInnerHTML("WebSocket连接成功");
}
//接收到消息的回调方法
websocket.onmessage = function (event) {
//setMessageInnerHTML(event.data);
var stat=event.data;
if(stat.startsWith('Time:')){
stat=event.data.substring(event.data.indexOf(":")+1,event.data.length);
myVideo.currentTime=stat;
myVideo.pause();
}
if(stat.startsWith('play')){
myVideo.play();
}
if(stat.startsWith('stop')){
myVideo.pause();
}
}
//连接关闭的回调方法
websocket.onclose = function () {
setMessageInnerHTML("WebSocket连接关闭");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function (event) {
closeWebSocket();
console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
console.log(e)
}
window.onunload = function() {
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭WebSocket连接
function closeWebSocket(event) {
websocket.close();
}
//发送消息
function send(message) {
websocket.send(message);
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function (event) {
closeWebSocket();
console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
console.log(e)
}
window.onunload = function() {
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭WebSocket连接
function closeWebSocket(event) {
websocket.close();
}
//发送消息
function send(message) {
websocket.send(message);
}
服务端控制:
package cn.jedi.WebSocket;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint.Async;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocket {
/**
* @Fields field:field:{TODO}(静态变量,用来记录当前在线连接数。应该把它设计成线程安全的)
*/
private static int onlineCount=0;
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识
private static CopyOnWriteArraySet<WebSocket> webSocketSet = new CopyOnWriteArraySet<WebSocket>();
//与某个客户端的连接会话
private Session session;
@OnOpen
public void onOpen(Session session) {
this.session=session;
webSocketSet.add(this);
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为:" + getOnlineCount());
}
@OnClose
public void OnClose() throws IOException {
session.close();
webSocketSet.remove(this);
subOnlineCount();
System.out.println("一条连接关闭!当前在线人数为:"+getOnlineCount());
}
@OnMessage
public void OnMessage(String message,Session session) {
String temp = null;
System.out.println("来自客户端的消息:"+message);
if (message.startsWith("时间:")) {
temp=message.substring(message.indexOf(":")+1,message.length());
for (WebSocket item:webSocketSet) {
try {
item.sendMessage("Time:"+temp);
} catch (Exception e) {
// TODO: handle exception
}
}
}
if (message.equals("play")) {
webSocketSet.remove(this);
for (WebSocket item:webSocketSet) {
try {
item.sendMessage("play");
} catch (Exception e) {
// TODO: handle exception
}
}
webSocketSet.add(this);
}
if (message.equals("stop")) {
//webSocketSet.remove(this);
for (WebSocket item:webSocketSet) {
try {
item.sendMessage("stop");
} catch (Exception e) {
// TODO: handle exception
}
}
//webSocketSet.add(this);
}
if (message.equals("wait")) {
for (WebSocket item:webSocketSet) {
try {
item.sendMessage("wait");
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
/**
* @Title: onError
* @Description: TODO(发生错误时调用)
* @param session
* @param error 设定文件
* @return void 返回类型
*/
@OnError
public void onError(Session session,Throwable error) {
System.out.println("发生错误");
error.printStackTrace();
}
/**
* @Title: sendMessage
* @Description: TODO(发送信息)
* @param message
* @throws IOException 设定文件
* @return void 返回类型
*/
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocket.onlineCount--;
}
}