Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add session lifecycle ApplicationEvent's
Issue: SPR-11578
- Loading branch information
1 parent
0745907
commit 13da705
Showing
6 changed files
with
297 additions
and
4 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...websocket/src/main/java/org/springframework/web/socket/messaging/SessionConnectEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2002-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.socket.messaging; | ||
|
||
|
||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Event raised when a new WebSocket client using a Simple Messaging Protocol | ||
* (e.g. STOMP) as the WebSocket sub-protocol issues a connect request. | ||
* | ||
* <p>Note that this is not the same as the WebSocket session getting established | ||
* but rather the client's first attempt to connect within the the sub-protocol, | ||
* for example sending the STOMP CONNECT frame. | ||
* | ||
* <p>The provided {@link #getMessage() message} can be examined to check | ||
* information about the connected user, The session id, and any headers | ||
* sent by the client, for STOMP check the class | ||
* {@link org.springframework.messaging.simp.stomp.StompHeaderAccessor}. | ||
* For example: | ||
* | ||
* <pre class="code"> | ||
* StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); | ||
* headers.getSessionId(); | ||
* headers.getSessionAttributes(); | ||
* headers.getPrincipal(); | ||
* </pre> | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0.3 | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class SessionConnectEvent extends ApplicationEvent { | ||
|
||
private final Message<byte[]> message; | ||
|
||
|
||
/** | ||
* Create a new SessionConnectEvent. | ||
* | ||
* @param source the component that published the event (never {@code null}) | ||
* @param message the connect message | ||
*/ | ||
public SessionConnectEvent(Object source, Message<byte[]> message) { | ||
super(source); | ||
Assert.notNull(message, "'message' must not be null"); | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* Return the connect message. | ||
*/ | ||
public Message<byte[]> getMessage() { | ||
return this.message; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "SessionConnectEvent: message=" + message; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...bsocket/src/main/java/org/springframework/web/socket/messaging/SessionConnectedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2002-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.socket.messaging; | ||
|
||
|
||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A connected event represents the server response to a client's connect request. | ||
* See {@link org.springframework.web.socket.messaging.SessionConnectEvent}. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0.3 | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class SessionConnectedEvent extends ApplicationEvent { | ||
|
||
private final Message<byte[]> message; | ||
|
||
|
||
/** | ||
* Create a new event. | ||
* | ||
* @param source the component that published the event (never {@code null}) | ||
* @param message the connected message | ||
*/ | ||
public SessionConnectedEvent(Object source, Message<byte[]> message) { | ||
super(source); | ||
Assert.notNull(message, "'message' must not be null"); | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* Return the connected message. | ||
*/ | ||
public Message<byte[]> getMessage() { | ||
return this.message; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "SessionConnectedEvent: message=" + message; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...socket/src/main/java/org/springframework/web/socket/messaging/SessionDisconnectEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2002-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.socket.messaging; | ||
|
||
|
||
import org.springframework.context.ApplicationEvent; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.socket.CloseStatus; | ||
|
||
/** | ||
* Event raised when the session of a WebSocket client using a Simple Messaging | ||
* Protocol (e.g. STOMP) as the WebSocket sub-protocol is closed. | ||
* | ||
* <p>Note that this event may be raised more than once for a single session and | ||
* therefore event consumers should be idempotent and ignore a duplicate event.. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 4.0.3 | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class SessionDisconnectEvent extends ApplicationEvent { | ||
|
||
private final String sessionId; | ||
|
||
private final CloseStatus status; | ||
|
||
/** | ||
* Create a new event. | ||
* | ||
* @param source the component that published the event (never {@code null}) | ||
* @param sessionId the disconnect message | ||
* @param closeStatus | ||
*/ | ||
public SessionDisconnectEvent(Object source, String sessionId, CloseStatus closeStatus) { | ||
super(source); | ||
Assert.notNull(sessionId, "'sessionId' must not be null"); | ||
this.sessionId = sessionId; | ||
this.status = closeStatus; | ||
} | ||
|
||
/** | ||
* Return the session id. | ||
*/ | ||
public String getSessionId() { | ||
return this.sessionId; | ||
} | ||
|
||
/** | ||
* Return the status with which the session was closed. | ||
*/ | ||
public CloseStatus getCloseStatus() { | ||
return this.status; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SessionDisconnectEvent: sessionId=" + this.sessionId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.