@@ -2,23 +2,26 @@ use super::handshake::request::{WebSocketRequest, ReadWebSocketRequest, WriteWeb
2
2
use super :: handshake:: response:: { WebSocketResponse , ReadWebSocketResponse , WriteWebSocketResponse } ;
3
3
use super :: message:: send:: { WebSocketSender , new_sender} ;
4
4
use super :: message:: receive:: { WebSocketReceiver , new_receiver} ;
5
- use std:: io:: net:: tcp:: TcpStream ;
6
- use std:: io:: net:: ip:: SocketAddr ;
7
- use std:: io:: { IoResult , IoError , IoErrorKind } ;
5
+ use std:: io:: { Stream , IoResult } ;
8
6
use std:: clone:: Clone ;
9
7
10
8
/// Represents a WebSocket client.
11
- /// To use WebSocketClient, you must create one using either WebSocketClient::connect(),
12
- /// which is used for writing clients, or WebSocketClient::from_stream(), which creates
13
- /// a WebSocketClient from a TcpStream (typically used in a server) .
9
+ /// To use WebSocketClient, you must create one using either WebSocketClient::new().
10
+ /// For a client, you can use TcpStream::connect() to connect to the server, then call
11
+ /// WebSocketClient::new() passing the resultant stream .
14
12
///
15
13
/// ```no_run
14
+ /// use std::io::TcpStream;
16
15
/// use websocket::WebSocketClient;
17
16
/// use websocket::handshake::WebSocketRequest;
18
17
///
19
18
/// let request = WebSocketRequest::new("ws://127.0.0.1:1234", ["myProtocol"].as_slice()).unwrap();
20
19
/// let key = request.key().unwrap();
21
- /// let mut client = WebSocketClient::connect(&request).unwrap();
20
+ ///
21
+ /// // We can get the hostname from the request
22
+ /// let stream = TcpStream::connect(request.host().unwrap().as_slice()).unwrap();
23
+ ///
24
+ /// let mut client = WebSocketClient::new(stream, true);
22
25
/// let response = client.receive_handshake_response().unwrap();
23
26
///
24
27
/// if !response.is_successful(key) {
@@ -31,62 +34,31 @@ use std::clone::Clone;
31
34
///
32
35
/// // ...
33
36
/// ```
34
- pub struct WebSocketClient {
35
- stream : TcpStream ,
37
+ pub struct WebSocketClient < S : Stream + Clone > {
38
+ stream : S ,
36
39
mask : bool ,
37
40
}
38
41
39
- impl WebSocketClient {
40
- /// Connect to the WebSocket server using the given request.
41
- /// Use WebSocketRequest::new() to create a request for use with this function.
42
- pub fn connect ( request : & WebSocketRequest ) -> IoResult < WebSocketClient > {
43
- let host = try!( request. headers . get ( "Host" ) . ok_or (
44
- IoError {
45
- kind : IoErrorKind :: InvalidInput ,
46
- desc : "No host specified" ,
47
- detail : None ,
48
- }
49
- ) ) ;
50
- //Connect to the server
51
- let mut stream = try!( TcpStream :: connect ( host. as_slice ( ) ) ) ;
52
- //Send the opening handshake
53
- try!( stream. write_websocket_request ( request) ) ;
54
-
55
- Ok ( WebSocketClient {
56
- stream : stream,
57
- mask : true ,
58
- } )
59
- }
60
-
61
- /// Creates a new WebSocketClient from a given TcpStream.
42
+ impl < S : Stream + Clone > WebSocketClient < S > {
43
+ /// Creates a new WebSocketClient from a given cloneable stream.
62
44
/// The mask parameter determines whether or not messages send to the remote endpoint will be masked.
63
45
/// If the client is connecting to a remote endpoint, set mask to true. If the client is the remote
64
46
/// endpoint (and therefore, the server is the local endpoint), set mask to false.
65
47
///
66
48
/// Nothing is sent to or read from the stream during the conversion.
67
- pub fn from_stream ( stream : TcpStream , mask : bool ) -> WebSocketClient {
49
+ pub fn new ( stream : S , mask : bool ) -> WebSocketClient < S > {
68
50
WebSocketClient {
69
51
stream : stream,
70
52
mask : mask,
71
53
}
72
54
}
73
55
74
- /// Returns a copy of the underlying TcpStream for this WebSocketClient.
56
+ /// Returns a copy of the underlying S for this WebSocketClient.
75
57
/// Note that writing to this stream will likely invalidate the WebSocket data stream.
76
- pub fn stream ( & self ) -> TcpStream {
58
+ pub fn stream ( & self ) -> S {
77
59
self . stream . clone ( )
78
60
}
79
61
80
- /// Returns the socket address of the remote peer of this TCP connection.
81
- pub fn peer_name ( & mut self ) -> IoResult < SocketAddr > {
82
- self . stream . peer_name ( )
83
- }
84
-
85
- /// Returns the socket address of the local half of this TCP connection.
86
- pub fn socket_name ( & mut self ) -> IoResult < SocketAddr > {
87
- self . stream . socket_name ( )
88
- }
89
-
90
62
/// Reads a request from this client. Only to be used if the server is the local endpoint and
91
63
/// the client is the remote endpoint.
92
64
pub fn receive_handshake_request ( & mut self ) -> IoResult < WebSocketRequest > {
@@ -99,6 +71,12 @@ impl WebSocketClient {
99
71
self . stream . read_websocket_response ( )
100
72
}
101
73
74
+ /// Sends the specified WebSocketRequest to the remote endpoint. Only to be used if the server is
75
+ /// the remote endpoint and the client is the local endpoint.
76
+ pub fn send_handshake_request ( & mut self , request : WebSocketRequest ) -> IoResult < ( ) > {
77
+ self . stream . write_websocket_request ( & request)
78
+ }
79
+
102
80
/// Sends the specified WebSocketResponse to this client. Only to be used if the server is
103
81
/// the local endpoint and the client is the remote endpoint.
104
82
pub fn send_handshake_response ( & mut self , response : WebSocketResponse ) -> IoResult < ( ) > {
@@ -107,37 +85,19 @@ impl WebSocketClient {
107
85
108
86
/// Returns a WebSocketSender from this client. Used to transmit data to the remote endpoint,
109
87
/// that is, to the server if WebSocketClient::connect() has been used, or to this client otherwise.
110
- pub fn sender ( & self ) -> WebSocketSender {
88
+ pub fn sender ( & self ) -> WebSocketSender < S > {
111
89
new_sender ( self . stream . clone ( ) , self . mask )
112
90
}
113
91
114
- /// Closes the sender for this WebSocketClient.
115
- /// This method will close the message sending portion of this client, causing all pending and future sends to immediately return with an error.
116
- /// This affects all WebSocketSenders for the client, and any copies of the underlying stream will be unable to write.
117
- ///
118
- /// Note that you should send a WebSocketMessage:Close message to the remote endpoint before calling this method.
119
- pub fn close_send ( & mut self ) -> IoResult < ( ) > {
120
- self . stream . close_write ( )
121
- }
122
-
123
92
/// Returns a WebSocketReceiver from this client. Used to receive data from the remote endpoint,
124
93
/// that is, from the server if WebSocketClient::connect() has been used, or from this client otherwise.
125
- pub fn receiver ( & self ) -> WebSocketReceiver {
94
+ pub fn receiver ( & self ) -> WebSocketReceiver < S > {
126
95
new_receiver ( self . stream . clone ( ) )
127
96
}
128
-
129
- /// Closes the receiver for this WebSocketClient.
130
- /// This method will close the message receiving portion of this client, causing all pending and future receives to immediately return with an error.
131
- /// This affects all WebSocketReceivers for the client, and any copies of the underlying stream will be unable to read.
132
- ///
133
- /// Note that you should send a WebSocketMessage:Close message to the remote endpoint before calling this method.
134
- pub fn close_receive ( & mut self ) -> IoResult < ( ) > {
135
- self . stream . close_read ( )
136
- }
137
97
}
138
98
139
- impl Clone for WebSocketClient {
140
- fn clone ( & self ) -> WebSocketClient {
99
+ impl < S : Stream + Clone > Clone for WebSocketClient < S > {
100
+ fn clone ( & self ) -> WebSocketClient < S > {
141
101
WebSocketClient {
142
102
stream : self . stream . clone ( ) ,
143
103
mask : self . mask ,
0 commit comments