forked from lets-blade/blade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lets-blade#321 from ydq/dev
websocket bug修复和调整
- Loading branch information
Showing
8 changed files
with
70 additions
and
34 deletions.
There are no files selected for viewing
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
40 changes: 30 additions & 10 deletions
40
src/main/java/com/blade/mvc/websocket/WebSocketContext.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 |
---|---|---|
@@ -1,27 +1,47 @@ | ||
package com.blade.mvc.websocket; | ||
|
||
import io.netty.channel.ChannelHandlerContext; | ||
import com.blade.mvc.handler.WebSocketHandler; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.experimental.Accessors; | ||
|
||
/** | ||
* @author biezhi | ||
* @author biezhi,darren | ||
* @date 2017/10/30 | ||
*/ | ||
@Data | ||
@Accessors(fluent = true) | ||
public class WebSocketContext { | ||
|
||
private ChannelHandlerContext ctx; | ||
@Getter | ||
private WebSocketSession session; | ||
private String reqText; | ||
@Getter | ||
private String message; | ||
private WebSocketHandler handler; | ||
|
||
public WebSocketContext(ChannelHandlerContext ctx) { | ||
this.ctx = ctx; | ||
this.session = new WebSocketSession(ctx.channel()); | ||
public WebSocketContext(WebSocketSession session,WebSocketHandler handler) { | ||
this.session = session; | ||
this.handler = handler; | ||
} | ||
public WebSocketContext(WebSocketSession session,WebSocketHandler handler,String message) { | ||
this(session,handler); | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* post a message | ||
* @param value | ||
*/ | ||
public void message(String value) { | ||
ctx.writeAndFlush(new TextWebSocketFrame(value)); | ||
session.handlerContext().writeAndFlush(new TextWebSocketFrame(value)); | ||
} | ||
|
||
/** | ||
* Allows the user to disconnect the websocket | ||
*/ | ||
public void disconnect(){ | ||
session.handlerContext().disconnect().addListener(ChannelFutureListener.CLOSE); | ||
handler.onDisConnect(this); | ||
} | ||
|
||
} |
16 changes: 9 additions & 7 deletions
16
src/main/java/com/blade/mvc/websocket/WebSocketSession.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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
package com.blade.mvc.websocket; | ||
|
||
import com.blade.kit.UUID; | ||
import io.netty.channel.Channel; | ||
import lombok.Data; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import lombok.Getter; | ||
import lombok.experimental.Accessors; | ||
|
||
/** | ||
* @author biezhi | ||
* @author biezhi,darren | ||
* @date 2017/10/30 | ||
*/ | ||
@Data | ||
@Getter | ||
@Accessors(fluent = true) | ||
public class WebSocketSession { | ||
|
||
private Channel channel; | ||
private ChannelHandlerContext handlerContext; | ||
private String uuid; | ||
|
||
public WebSocketSession(Channel channel) { | ||
this.channel = channel; | ||
public WebSocketSession(ChannelHandlerContext handlerContext) { | ||
this.handlerContext = handlerContext; | ||
this.uuid = UUID.UU32(); | ||
} | ||
} |
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
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
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