Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12743 > unrolled thread

How to receive and send websocket messages in Java?

Started byMihael <isaev.mihael@gmail.com>
First post2012-03-07 12:02 -0800
Last post2012-03-07 14:24 -0800
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  How to receive and send websocket messages in Java? Mihael <isaev.mihael@gmail.com> - 2012-03-07 12:02 -0800
    Re: How to receive and send websocket messages in Java? Mihael <isaev.mihael@gmail.com> - 2012-03-07 12:03 -0800
    Re: How to receive and send websocket messages in Java? Lew <lewbloch@gmail.com> - 2012-03-07 13:17 -0800
      Re: How to receive and send websocket messages in Java? Mihael <isaev.mihael@gmail.com> - 2012-03-07 14:08 -0800
        Re: How to receive and send websocket messages in Java? Knute Johnson <nospam@knutejohnson.com> - 2012-03-07 14:24 -0800

#12743 — How to receive and send websocket messages in Java?

FromMihael <isaev.mihael@gmail.com>
Date2012-03-07 12:02 -0800
SubjectHow to receive and send websocket messages in Java?
Message-ID<31969353.4397.1331150560621.JavaMail.geo-discussion-forums@vbtv42>
Hello!

I try to create websocket server on Java

this is my draft of code http://pastebin.com/k1D46cV7

i try to connect from Chrome on Mac version 17.0.963.66
from this test page http://websocket.org/echo.html

And connection success

But when i send any data to server, in console on server application i see string like 

БУЭѓчЮ’ ЫттПДсюƒТкљ№Тмл Е

how can i decode this string to normal?

And when i try to send answer from server - client not get message...

Please, give me code for right receive and decode message and right encode and send message.

Big thanks :)

----

I found documentation http://tools.ietf.org/agenda/80/slides/hybi-2.pdf but can't understand.. 

[toc] | [next] | [standalone]


#12744

FromMihael <isaev.mihael@gmail.com>
Date2012-03-07 12:03 -0800
Message-ID<23615891.627.1331150634979.JavaMail.geo-discussion-forums@vbfl9>
In reply to#12743
P.S. Sorry for bad english..

[toc] | [prev] | [next] | [standalone]


#12749

FromLew <lewbloch@gmail.com>
Date2012-03-07 13:17 -0800
Message-ID<24681763.594.1331155027592.JavaMail.geo-discussion-forums@pbcsl10>
In reply to#12743
On Wednesday, March 7, 2012 12:02:40 PM UTC-8, Mihael wrote:
> Hello!
> 
> I try to create websocket server on Java
> 
> this is my draft of code http://pastebin.com/k1D46cV7
> 
> i try to connect from Chrome on Mac version 17.0.963.66
> from this test page http://websocket.org/echo.html
> 
> And connection success
> 
> But when i send any data to server, in console on server application i see string like 
> 
> БУЭѓчЮ’ ЫттПДсюƒТкљ№Тмл Е

That's because you aren't controlling the string encoding.

> how can i [sic] decode this string to normal?

What is "normal"?

> And when i try to send answer from server - client not get message...
> 
> Please, give me code for right receive and decode message and right encode and send message.

What is the native platform encoding on the server side?

Always include encoding explicitly in the calls to encode/decode strings.

Don't manipulate bytes to make Strings. Even 'char' isn't safe, as code points can be wider than 16 bits.

Don't hard-code numeric values for end-of-line.

Use a 'Reader' and 'Writer' instead of raw streams.

Spell "receive" correctly.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#12751

FromMihael <isaev.mihael@gmail.com>
Date2012-03-07 14:08 -0800
Message-ID<4309843.3.1331158120887.JavaMail.geo-discussion-forums@vblo18>
In reply to#12749
Thanks for your answer! But i can't understand, how to decode БУЭѓчЮ’ ЫттПДсюƒТкљ№Тмл Е  ?

When I send "1" i get string ББђ,¶3Э
or send "ping" i get string БДЧ≤’4зџїS
or send "hello world!" i get string БМяЩрЈfхЬ∞#оЯ≠oэ—

this is special encoded websocket strings? or what?

pseudo code is

InputStream is = s.getInputStream();
byte buf[] = new byte[64*1024];
int r = is.read(buf);
String data = new String(buf, 0, r);
System.out.println("Recieved Data: " + data);

[toc] | [prev] | [next] | [standalone]


#12753

FromKnute Johnson <nospam@knutejohnson.com>
Date2012-03-07 14:24 -0800
Message-ID<jj8n6j$c0e$1@dont-email.me>
In reply to#12751
On 3/7/2012 2:08 PM, Mihael wrote:
> Thanks for your answer! But i can't understand, how to decode БУЭѓчЮ’ ЫттПДсюƒТкљ№Тмл Е  ?
>
> When I send "1" i get string ББђ,¶3Э
> or send "ping" i get string БДЧ≤’4зџїS
> or send "hello world!" i get string БМяЩрЈfхЬ∞#оЯ≠oэ—
>
> this is special encoded websocket strings? or what?
>
> pseudo code is
>
> InputStream is = s.getInputStream();
> byte buf[] = new byte[64*1024];
> int r = is.read(buf);
> String data = new String(buf, 0, r);
> System.out.println("Recieved Data: " + data);

If you are going to send Strings, you don't want to read and write bytes.

BufferedReader br = new BufferedReader(new InputStreamReader(
  s.getInputStream(),***YOUR CHAR SET GOES HERE***));

br.readLine();

If the server you are connecting to is using a different character set 
you need to use a different constructor for InputStreamReader that sets 
the character set.  See the docs for InputStreamReader.

-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web