Commit b4d10420 by huyi

添加可以获取websocket——httpsession的类,test.html为前端测试页面

parent 8e2f7b07
package com.fhkj.oltinspection.config;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
@Component
public class RequestListener implements ServletRequestListener {
public void requestInitialized(ServletRequestEvent sre) {
//将所有request请求都携带上httpSession
((HttpServletRequest) sre.getServletRequest()).getSession();
}
public RequestListener() {
}
public void requestDestroyed(ServletRequestEvent arg0) {
}
}
\ No newline at end of file
......@@ -39,23 +39,20 @@ public class PushInfoController {
public void onOpen(Session session, EndpointConfig config) {
this.session = session;
HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
System.out.println("name is : "+ httpSession.getAttribute("name"));
/*System.out.println("name is : "+ httpSession.getAttribute("name"));
System.out.println("the session is : "+session.getId());
System.out.println("session content is : "+session.getId());
System.out.println("httpSession is : "+httpSession.getId());
System.out.println("httpSession is : "+httpSession.getId()); */
// 获取路径传过来的username
String str = session.getQueryString();
System.out.println(str + "======");
String sendUser = str.split("=")[1];
map.put(sendUser, session);
webSocketSet.add(this); // 加入set中
addOnlineCount(); // 在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount() + "连接人id是:" + sendUser);
try {
sendMessage(""); // 发送消息的方法
sendMessage("已接收" + sendUser + "的消息"); // 发送消息的方法
} catch (IOException e) {
System.out.println("IO异常");
}
......@@ -92,7 +89,13 @@ public class PushInfoController {
*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
try {
System.out.println("来自客户端的消息:" + message);
sendMessage("我收到了你的消息");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 发送消息的方法
}
/**
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>testWebSocket</title>
</head>
<body>
<button id = "send">发送</button>
<script>
var websocket = null;
//判断当前浏览器是否支持WebSocket
if('WebSocket' in window) {
websocket = new WebSocket("ws://localhost:17290/oltinspection/pushInfo?name=" + "123");
} else {
alert('Not support websocket')
}
//连接发生错误的回调方法
websocket.onerror = function() {
setMessageInnerHTML("error");
};
//连接成功建立的回调方法
websocket.onopen = function(event) {
setMessageInnerHTML("open");
}
//接收到消息的回调方法
websocket.onmessage = function(event) {
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function() {
setMessageInnerHTML("close");
}
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
window.onbeforeunload = function() {
websocket.close();
}
//将消息显示在网页上
function setMessageInnerHTML(message) {
alert(message);
// document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//关闭连接
function closeWebSocket() {
websocket.close();
}
//发送消息
function send() {
var message = "我和你说话了";
alert(message);
websocket.send(message);
}
document.onclick = function () {
send();
}
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment