本文整理汇总了Java中org.eclipse.jetty.websocket.WebSocket类的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocket类属于org.eclipse.jetty.websocket包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
if (isStopped) {
return null;
}
// FIXME: Replace this with globally shared opaque token to allow secure failover
Principal p = request.getUserPrincipal();
String userName = p != null ? p.getName() : FAKE_USERNAME;
UiWebSocket socket = new UiWebSocket(directory, userName);
synchronized (sockets) {
sockets.add(socket);
}
return socket;
}
示例2: toWebSocketConnection
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
WebSocketConnection toWebSocketConnection(final WebSocket.Connection connection) {
return new WebSocketConnection() {
@Override
public void write(String data) throws IOException {
connection.sendMessage(data);
}
@Override
public void disconnect() {
connection.disconnect();
}
@Override
public boolean isOpen() {
return connection.isOpen();
}
@Override
public void setMaxIdleTime(int ms) {
connection.setMaxIdleTime(ms);
}
};
}
示例3: createNote
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
private void createNote(WebSocket conn, Notebook notebook, Message message) throws IOException {
Note note = notebook.createNote();
note.addParagraph(); // it's an empty note. so add one paragraph
if (message != null) {
String noteName = (String) message.get("name");
if (noteName == null || noteName.isEmpty()){
noteName = "Note " + note.getId();
}
note.setName(noteName);
}
note.persist();
addConnectionToNote(note.id(), (NotebookSocket) conn);
broadcastNote(note);
broadcastNoteList();
}
示例4: WebSocketServer
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
public WebSocketServer(int port, String rootDirPath, final MainWindow m, final Statement smt, final java.sql.Connection sqlcon) {
idCounter = 0;
connector = new SelectChannelConnector();
connector.setPort(port);
addConnector(connector);
wsHandler = new WebSocketHandler() {
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request,
String protocol) {
idCounter++;
System.out.printf("Protocol:%s\n", protocol);
return new MyWebSocket(idCounter, m, smt, sqlcon);
}
};
resHandler = new ResourceHandler();
resHandler.setDirectoriesListed(true);
resHandler.setResourceBase(rootDirPath);
wsHandler.setHandler(resHandler);
setHandler(wsHandler);
}
开发者ID:ryohashioka,项目名称:Visual-Programming-Environment-for-Coordinating-Appliances-and-Services-in-a-Smart-House,代码行数:25,代码来源:WebSocketServer.java
示例5: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
// determine if we need to send in binary
boolean binary = false;
if(protocol.equals(PROTOCOL_BSON)) {
binary = true;
}
// get the object mapper
ObjectMapper mapper = getMapper(request, protocol);
if(mapper == null) {
throw new RuntimeException("Unknown protocol " + protocol);
}
// create transport
JsonRpcWsTransport transport = new JsonRpcWsTransport(mapper, true);
transport.setAcceptTextMessages(!binary);
transport.setSendBinaryMessages(binary);
// get server and handler
JsonRpcServer server = getServer(request, protocol);
Object handler = getHandler(request, protocol);
// bind the transport and return it
transport.bindServer(server, handler);
return transport;
}
示例6: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
/**
* 构造模块的WebSocket通讯连接
* <p>对应的模块必须实现了{@link WebSocketAcceptor}接口</p>
* <p>访问的路径为/sandbox/module/websocket/MODULE_NAME</p>
*
* @param req req
* @param protocol websocket protocol
* @return WebSocket
*/
@Override
public WebSocket doWebSocketConnect(final HttpServletRequest req,
final String protocol) {
final String uniqueId = parseUniqueId(req.getPathInfo());
if (StringUtils.isBlank(uniqueId)) {
logger.warn("websocket value={} is illegal.", req.getPathInfo());
return null;
}
final CoreModule coreModule = coreModuleManager.get(uniqueId);
if (null == coreModule) {
logger.warn("module[id={};] was not existed.", uniqueId);
return null;
}
if (!(coreModule.getModule() instanceof WebSocketAcceptor)) {
logger.warn("module[id={};class={};] is not implements WebSocketAcceptor.",
uniqueId, coreModule.getModule().getClass());
return null;
}
final WebSocketConnectionListener listener =
((WebSocketAcceptor) coreModule.getModule()).onAccept(req, protocol);
logger.info("accept websocket connection, module[id={};class={};], value={};",
uniqueId, coreModule.getModule().getClass(), req.getPathInfo());
if (listener instanceof TextMessageListener) {
return new InnerOnTextMessage(uniqueId, (TextMessageListener) listener);
} else {
return new InnerWebSocket(uniqueId, listener);
}
}
示例7: execute
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public Object execute(ActionArgumentAggregatorFactory.ActionArgumentAggregator aggregator) throws Exception {
HttpServletRequest request = aggregator.find(HttpServletRequest.class, InstanceSource.Request);
HttpServletResponse response = aggregator.find(HttpServletResponse.class, InstanceSource.Request);
WebSocket webSocket = socketImplDelegate.apply(aggregator);
WebSocketFactory.Acceptor acceptor = WebSocketAcceptorFactory.factorize(webSocket);
new WebSocketFactory(acceptor).acceptWebSocket(request, response);
return ActionResult.NoOp();
}
示例8: updateNote
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
private void updateNote(WebSocket conn, Notebook notebook, Message fromMessage)
throws SchedulerException, IOException {
String noteId = (String) fromMessage.get("id");
String name = (String) fromMessage.get("name");
Map<String, Object> config = (Map<String, Object>) fromMessage
.get("config");
if (noteId == null) {
return;
}
if (config == null) {
return;
}
Note note = notebook.getNote(noteId);
if (note != null) {
boolean cronUpdated = isCronUpdated(config, note.getConfig());
note.setName(name);
note.setConfig(config);
if (cronUpdated) {
notebook.refreshCron(note.id());
}
note.persist();
broadcastNote(note);
broadcastNoteList();
}
}
示例9: removeNote
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
private void removeNote(WebSocket conn, Notebook notebook, Message fromMessage)
throws IOException {
String noteId = (String) fromMessage.get("id");
if (noteId == null) {
return;
}
Note note = notebook.getNote(noteId);
notebook.removeNote(noteId);
removeNote(noteId);
broadcastNoteList();
}
示例10: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
{
@SuppressWarnings("unchecked")
PRINCIPAL principal = (PRINCIPAL)request.getAttribute(authAttribute);
return new PubSubWebSocket(principal);
}
示例11: start
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
public void start() throws Exception
{
server = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(port);
server.addConnector(connector);
// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/");
server.setHandler(contextHandler);
WebSocketServlet webSocketServlet = new WebSocketServlet()
{
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
{
return websocket;
}
};
contextHandler.addServlet(new ServletHolder(webSocketServlet), "/pubsub");
server.start();
if (port == 0) {
port = server.getConnectors()[0].getLocalPort();
}
}
示例12: simpleTest
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Test
public void simpleTest() throws Exception
{
final int port = 6666;
String connectionURI = "ws://localhost:" + port + WebSocketServerInputOperator.DEFAULT_EXTENSION;
final String message = "hello world";
WebSocketServerInputOperator wssio = new TestWSSIO();
wssio.setPort(port);
wssio.setup(null);
WebSocketClientFactory clientFactory = new WebSocketClientFactory();
clientFactory.start();
WebSocketClient client = new WebSocketClient(clientFactory);
Future<WebSocket.Connection> connectionFuture = client.open(new URI(connectionURI), new TestWebSocket());
WebSocket.Connection connection = connectionFuture.get(5, TimeUnit.SECONDS);
connection.sendMessage(message);
long startTime = System.currentTimeMillis();
while (startTime + 10000 > System.currentTimeMillis()) {
if (TestWSSIO.messages.size() >= 1) {
break;
}
Thread.sleep(100);
}
Assert.assertEquals("The number of messages recieved is incorrect.", 1, TestWSSIO.messages.size());
Assert.assertEquals("Incorrect message received", message, TestWSSIO.messages.get(0));
connection.close();
wssio.teardown();
}
示例13: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
TopologyViewWebSocket socket = new TopologyViewWebSocket(directory);
synchronized (sockets) {
sockets.add(socket);
}
return socket;
}
示例14: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
UiWebSocket socket = new UiWebSocket(directory);
synchronized (sockets) {
sockets.add(socket);
}
return socket;
}
示例15: doWebSocketConnect
import org.eclipse.jetty.websocket.WebSocket; //导入依赖的package包/类
@Override
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
WebSocket socket;
if (protocol.startsWith("mqtt")) {
socket = new MQTTSocket();
} else {
socket = new StompSocket();
}
listener.onAccept((Transport)socket);
return socket;
}