ASF Bugzilla – Attachment 33111 Details for
Bug 58414
Can not transfer empty string through Web Socket
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Example of ServerEndpoint implementation.
WsViewer.java (text/plain), 2.09 KB, created by
Evgeny Mironenko
on 2015-09-15 21:49:38 UTC
(
hide
)
Description:
Example of ServerEndpoint implementation.
Filename:
MIME Type:
Creator:
Evgeny Mironenko
Created:
2015-09-15 21:49:38 UTC
Size:
2.09 KB
patch
obsolete
>import org.apache.log4j.Logger; > >import javax.websocket.*; >import javax.websocket.server.PathParam; >import javax.websocket.server.ServerEndpoint; >import java.io.IOException; >import java.util.Set; >import java.util.concurrent.CopyOnWriteArraySet; > >@ServerEndpoint(value = "/chat/{room}") >public class WsViewer { > > private static final Logger LOG = Logger.getLogger(WsViewer.class); > private static final String ROOM = "room"; > private static final Set<WsViewer> connections = new CopyOnWriteArraySet<>(); > private Session session; > > @OnOpen > public void onOpen(Session session, @PathParam(ROOM) String room) { > session.getUserProperties().put(ROOM, room); > this.session = session; > connections.add(this); > } > > @OnClose > public void onClose() { > connections.remove(this); > System.out.println(session.getId() + " has been disconnected"); > } > > /** > * Sends the message trough websocket. > * @param msg message, which should be delivered. > */ > @OnMessage > public void sendMessage(String msg) { > broadcast(msg); > } > > @OnError > public void logError(Throwable throwable) { > LOG.error("Exception occurred during data transmission through Web Socket ", throwable); > } > > private void broadcast(String msg) { > for (WsViewer client : connections) { > try { > synchronized (client) { > String currentRoom = (String) session.getUserProperties().get(ROOM); > if (currentRoom.equals(client.session.getUserProperties().get(ROOM))) { > client.session.getBasicRemote().sendText(msg); > } > } > } catch (IOException e) { > LOG.error("Exception occurred during data transmission through Web Socket ", e); > connections.remove(client); > try { > client.session.close(); > } catch (IOException e1) { > // Ignore > } > } > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 58414
: 33111