This is probably obvious, but I am new to this paradigm. I am creating a Jetty server and following my websacet class as follows Registering:
server server = new server (8080); WebSocketHandler wsHandler = New WebSketchesHandler () {@Override Configure Public Zero (); factory.register (MyEchoSocket.class); }}; Server.setHandler (wsHandler); WebSut is recovering the message. Without getting the message before the client, I would also like to be able to send messages to the server. How can the MyEchoSocket instance be used when opening the connection? Or, more generally, how do I send a message to the socket outside the onText method in MyEchoSocket?
Two common techniques have been presented in a super-simplified chat room concept.
Option # 1: Revert the report of the websit to its central location @ WebSocket Public Class ChatSocket {public session session; Connect to Public Zero on @OnWebSocketConnect (session session) {this.Session = session; . Join ChatRoom.getInstance () (this); } @OnWebSocketMessage Lessons on public spell (string message) {ChatRoom.getInstance (). WriteAllMembers ("All Hello"); } Close public zeros on @OnWebSocketClose (int status code, reason of string) {ChatRoom.getInstance (). Part (this); }} Public Square ChatRoom {Private Fixed Last ChatRoom INSTANCE = New ChatRoom (); Public Stats ChatRoom getInstance () {return INSTANCE; } Private listing & lt; ChatSocket & gt; Members = new Arrestists & lt; & Gt; (); Public zeros included (chatset socket) {members.add (socket); } Public Zero part (chatset socket) {members.remove (socket); } Public Zero writing AllMembers (string message) {for (ChatSocket member: member) {member.session.getRemote () SendStringByFuture (message); }} Write a public null specific member (string member name, string message) {ChatSocket member = findMemberByName (memberName); . Member.session.getRemote () sendStringByFuture (message); } Public ChatSocketNetBarName (string username) {// reader left as exercise}} Then just use the central location to talk to your favorite website.
ChatRoom.getInstance (). Write Questions ("Alex", "Hello"); // or ChatRoom.getInstance (). WriteAllMembers ("All Hello"); Option # 2: WebSocket is created with WebSocketCreator @ WebSocket Public Class ChatSocket {public chat room chat room; Public Chatkets (chat room chat rooms) {this.chatroom = chatroom; } Connect to Public Zero on @OnWebSocketConnect (session session) {chatroom.join (this); } @OnWebSocketMessage Lessons on Public Zero (String Message) {chatroom.writeAllMembers (Message); } Close @OnSubasocket Close public zeros (int status code, reason of string) {chatroom.part (this); }} Public class ChatCreator WebSocketCreator {Private ChatRoom applies chatroom; Public chat creator (chat room chat room) {this.chatroom = chatroom; } Public Object createWebSocket (Upgrade Required Request, Upgrade Response Response) {// We want to create a chat socket and with our chat room implementation // want to return it to a new ChatSocket (chat room); }} Public Square Chathandler WebSocketHandler [Private ChatRoom chatroom = New ChatRoom ()) Expands; @ Override Public Wide Configurable (WebSocket Certificate Factory) {factory.setCreator (new chat creator (chat room)); }} At this point you can use the above technique to talk to your favorite web sites.
No comments:
Post a Comment