Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
oltinspection
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
胡懿
oltinspection
Commits
b4d10420
Commit
b4d10420
authored
Mar 07, 2019
by
huyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加可以获取websocket——httpsession的类,test.html为前端测试页面
parent
8e2f7b07
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
7 deletions
+103
-7
RequestListener.java
...n/java/com/fhkj/oltinspection/config/RequestListener.java
+23
-0
PushInfoController.java
...com/fhkj/oltinspection/controller/PushInfoController.java
+10
-7
test.html
src/main/resources/test.html
+70
-0
No files found.
src/main/java/com/fhkj/oltinspection/config/RequestListener.java
0 → 100644
View file @
b4d10420
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
src/main/java/com/fhkj/oltinspection/controller/PushInfoController.java
View file @
b4d10420
...
...
@@ -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
();
}
// 发送消息的方法
}
/**
...
...
src/main/resources/test.html
0 → 100644
View file @
b4d10420
<!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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment