Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
涂亚平
/
subsidy
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2c9d89b2
authored
Jul 15, 2022
by
邓敏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WebSocket
1 parent
72047ffc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
4 deletions
src/main/java/com/subsidy/util/IpAddressUtil.java
src/main/java/com/subsidy/util/websocket/WebSocketUtil.java
src/main/java/com/subsidy/util/IpAddressUtil.java
0 → 100644
View file @
2c9d89b
package
com
.
subsidy
.
util
;
import
javax.servlet.http.HttpServletRequest
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
public
class
IpAddressUtil
{
public
static
String
getIpAddress
(
HttpServletRequest
request
)
{
String
ipAddress
=
request
.
getHeader
(
"x-forwarded-for"
);
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ipAddress
==
null
||
ipAddress
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ipAddress
))
{
ipAddress
=
request
.
getRemoteAddr
();
if
(
ipAddress
.
equals
(
"127.0.0.1"
)
||
ipAddress
.
equals
(
"0:0:0:0:0:0:0:1"
))
{
InetAddress
inet
=
null
;
try
{
inet
=
InetAddress
.
getLocalHost
();
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
}
ipAddress
=
inet
.
getHostAddress
();
}
}
if
(
ipAddress
!=
null
&&
ipAddress
.
length
()
>
15
)
{
if
(
ipAddress
.
indexOf
(
","
)
>
0
)
{
ipAddress
=
ipAddress
.
substring
(
0
,
ipAddress
.
indexOf
(
","
));
}
}
return
ipAddress
;
}
}
src/main/java/com/subsidy/util/websocket/WebSocketUtil.java
View file @
2c9d89b
package
com
.
subsidy
.
util
.
websocket
;
package
com
.
subsidy
.
util
.
websocket
;
import
com.subsidy.mapper.OprMemDictMapper
;
import
com.subsidy.model.OprMemDictDO
;
import
com.subsidy.util.DateFormatUtil
;
import
com.subsidy.util.DateFormatUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.socket.CloseStatus
;
import
org.springframework.web.socket.CloseStatus
;
import
org.springframework.web.socket.TextMessage
;
import
org.springframework.web.socket.TextMessage
;
...
@@ -23,6 +26,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
...
@@ -23,6 +26,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Component
@Component
public
class
WebSocketUtil
implements
WebSocketHandler
{
public
class
WebSocketUtil
implements
WebSocketHandler
{
@Autowired
private
OprMemDictMapper
oprMemDictMapper
;
/**
/**
* 存放建立连接webSocket对象
* 存放建立连接webSocket对象
*/
*/
...
@@ -74,10 +80,20 @@ public class WebSocketUtil implements WebSocketHandler {
...
@@ -74,10 +80,20 @@ public class WebSocketUtil implements WebSocketHandler {
*/
*/
@Override
@Override
public
void
afterConnectionClosed
(
WebSocketSession
session
,
CloseStatus
closeStatus
)
throws
Exception
{
public
void
afterConnectionClosed
(
WebSocketSession
session
,
CloseStatus
closeStatus
)
throws
Exception
{
webSocketMap
.
remove
(
session
);
if
(
null
!=
session
)
{
System
.
out
.
println
(
"<---- webSocket is close"
);
webSocketMap
.
remove
(
session
);
log
.
info
(
"<---- webSocket is close"
);
log
.
info
(
"session {} close, status: {}"
,
session
.
getId
(),
closeStatus
);
OprMemDictDO
oprMemDictDO
=
new
OprMemDictDO
();
String
params
=
session
.
getUri
().
getQuery
();
String
[]
id
=
params
.
split
(
"="
);
oprMemDictDO
.
setUserId
(
Long
.
valueOf
(
id
[
1
]));
oprMemDictDO
.
setOprType
(
"登出"
);
oprMemDictDO
.
setIpAddress
(
session
.
getRemoteAddress
().
getHostName
());
oprMemDictMapper
.
insert
(
oprMemDictDO
);
System
.
out
.
println
(
"<---- webSocket is close"
);
log
.
info
(
"<---- webSocket is close"
);
log
.
info
(
"session {} close, status: {}"
,
session
.
getId
(),
closeStatus
);
}
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment