move |message|
shades the message
variable that you specified outside of closure. So in closing .. message
is called ws::Message
... except that you did this:
message = message.trim();
The compiler goes "oh no! trim()
? This does not exist for ws::Message
" .. and now it does not quite know what to do.
Option 1
The first fix involves delegating a trim()
call to the client who sends the message.
The fix is ββto not make any assumptions that the message is inside this closure. If you save this:
move |message|
.. but remove the trim()
call, the compiler will happily pass it the ws::Message
type and build:
if let Err(error) = listen("127.0.0.1:3012", |out| { // The handler needs to take ownership of out, so we use move move |message| { // --- REMOVED trim() call --- // Handle messages received on this connection println!("Server got message '{}'. ", message); // Use the out channel to send messages back out.send(message) } }
This gives you the ability to delegate a trim()
call to the client.
Option 2
Option 2 includes checking the type of message you received and make sure that you cut it only if it is text:
// The handler needs to take ownership of out, so we use move move |mut message: ws::Message| { // Only do it if the Message is text if message.is_text() { message = ws::Message::Text(message.as_text().unwrap().trim().into()); } // Handle messages received on this connection println!("Server got message '{}'. ", message); // Use the out channel to send messages back out.send(message) }
This may be a little more verbose than necessary ... but hopefully it will show you what the actual problem is with your source code snippet.
source share