View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.sockjs.client;
18  
19  import java.net.InetSocketAddress;
20  import java.net.URI;
21  import java.util.List;
22  
23  import org.springframework.util.Assert;
24  import org.springframework.util.concurrent.SettableListenableFuture;
25  import org.springframework.web.socket.CloseStatus;
26  import org.springframework.web.socket.TextMessage;
27  import org.springframework.web.socket.WebSocketExtension;
28  import org.springframework.web.socket.WebSocketHandler;
29  import org.springframework.web.socket.WebSocketSession;
30  import org.springframework.web.socket.sockjs.transport.TransportType;
31  
32  
33  /**
34   * An extension of {@link AbstractClientSockJsSession} for use with HTTP
35   * transports simulating a WebSocket session.
36   *
37   * @author Rossen Stoyanchev
38   * @since 4.1
39   */
40  public class XhrClientSockJsSession extends AbstractClientSockJsSession {
41  
42  	private final URI sendUrl;
43  
44  	private final XhrTransport transport;
45  
46  	private int textMessageSizeLimit = -1;
47  
48  	private int binaryMessageSizeLimit = -1;
49  
50  
51  	public XhrClientSockJsSession(TransportRequest request, WebSocketHandler handler,
52  			XhrTransport transport, SettableListenableFuture<WebSocketSession> connectFuture) {
53  
54  		super(request, handler, connectFuture);
55  		Assert.notNull(transport, "'restTemplate' is required");
56  		this.sendUrl = request.getSockJsUrlInfo().getTransportUrl(TransportType.XHR_SEND);
57  		this.transport = transport;
58  	}
59  
60  
61  	@Override
62  	public InetSocketAddress getLocalAddress() {
63  		return null;
64  	}
65  
66  	@Override
67  	public InetSocketAddress getRemoteAddress() {
68  		return new InetSocketAddress(getUri().getHost(), getUri().getPort());
69  	}
70  
71  	@Override
72  	public String getAcceptedProtocol() {
73  		return null;
74  	}
75  
76  	@Override
77  	public void setTextMessageSizeLimit(int messageSizeLimit) {
78  		this.textMessageSizeLimit = messageSizeLimit;
79  	}
80  
81  	@Override
82  	public int getTextMessageSizeLimit() {
83  		return this.textMessageSizeLimit;
84  	}
85  
86  	@Override
87  	public void setBinaryMessageSizeLimit(int messageSizeLimit) {
88  		this.binaryMessageSizeLimit = -1;
89  	}
90  
91  	@Override
92  	public int getBinaryMessageSizeLimit() {
93  		return this.binaryMessageSizeLimit;
94  	}
95  
96  	@Override
97  	public List<WebSocketExtension> getExtensions() {
98  		return null;
99  	}
100 
101 	@Override
102 	protected void sendInternal(TextMessage message) {
103 		this.transport.executeSendRequest(this.sendUrl, message);
104 	}
105 
106 	@Override
107 	protected void disconnect(CloseStatus status) {
108 		// Nothing to do, XHR transports check if session is disconnected
109 	}
110 
111 }