Giter Club home page Giter Club logo

android-websockets's Introduction

WebSocket client for Android

A very simple bare-minimum WebSocket client for Android.

Credits

The hybi parser is based on code from the faye project. Faye is Copyright (c) 2009-2012 James Coglan. Many thanks for the great open-source library!

Ported from JavaScript to Java by Eric Butler [email protected].

Usage

Here's the entire API:

List<BasicNameValuePair> extraHeaders = Arrays.asList(
    new BasicNameValuePair("Cookie", "session=abcd");
);

WebSocketClient client = new WebSocketClient(URI.create("wss://irccloud.com"), new WebSocketClient.Handler() {
    @Override
    public void onConnect() {
        Log.d(TAG, "Connected!");
    }

    @Override
    public void onMessage(String message) {
        Log.d(TAG, String.format("Got string message! %s", message));
    }

    @Override
    public void onMessage(byte[] data) {
        Log.d(TAG, String.format("Got binary message! %s", toHexString(data));
    }

    @Override
    public void onDisconnect(int code, String reason) {
        Log.d(TAG, String.format("Disconnected! Code: %d Reason: %s", code, reason));
    }

    @Override
    public void onError(Exception error) {
        Log.e(TAG, "Error!", error);
    }
}, extraHeaders);

client.connect();

// Later… 
client.send("hello!");
client.send(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF });
client.disconnect();

TODO

License

(The MIT License)

Copyright (c) 2009-2012 James Coglan
Copyright (c) 2012 Eric Butler 

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

android-websockets's People

Contributors

codebutler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-websockets's Issues

Disconnect method called immediately after onConnect called

I have a web socket connected in app. Now app need to connect to different web socket url. But for second connection I after onconnect method called it immediately called ondisconnect method twice with error code 744 and EOF message respectively. Did anyone faced same issue and solved it?

Usage of deprecated library org.apache.http

StatusLine, HttpException, Header and BasicLineParser are all part of deprecated
org.apache.http. Are there plans to re-write this class using the java.net.HttpURLConnection?

Thanks for code. Maybe you interested in proposed improvements:

  • no need HappyDataInputStream extends DataInputStream,
    if you use DataInputStream readFully method: stream.readFully(...) to read from input stream exactly some number
    of bytes (not less as it can be if you use read() )

  • you use double arithmetic in the code (seems it's java script way But in Java we can work with bits fields.
    So instead:
    frame[1] = (byte) (masked | 127);
    frame[2] = (byte) (((int) Math.floor(length / Math.pow(2, 56))) & BYTE);
    frame[3] = (byte) (((int) Math.floor(length / Math.pow(2, 48))) & BYTE);
    frame[4] = (byte) (((int) Math.floor(length / Math.pow(2, 40))) & BYTE);
    frame[5] = (byte) (((int) Math.floor(length / Math.pow(2, 32))) & BYTE);
    frame[6] = (byte) (((int) Math.floor(length / Math.pow(2, 24))) & BYTE);
    frame[7] = (byte) (((int) Math.floor(length / Math.pow(2, 16))) & BYTE);
    frame[8] = (byte) (((int) Math.floor(length / Math.pow(2, 8))) & BYTE);
    frame[9] = (byte) (length & BYTE);

    can be used:
    frame[1] = (byte) (masked | 127);
    //frame[2] = (byte) (0);
    //frame[3] = (byte) (0);
    //frame[4] = (byte) (0);
    //frame[5] = (byte) (0);
    frame[6] = (byte) ((length >> 24) & 0xFF);
    frame[7] = (byte) ((length >> 16) & 0xFF);
    frame[8] = (byte) ((length >> 8) & 0xFF);
    frame[9] = (byte) (length & 0xFF);

    • variable 'length' has int type, so contains only 4 bytes, so
      frame[2]..frame[5] will be always 0.
      (your code ported from java script and wrote as length can contains 8 bytes)

    • you can use DataInputStream standard function to read 2 and 8 bytes integer, likes:
      length = stream.readUnsignedShort(); // read 2 bytes length

      long length8 = stream.readLong(); // read 8 bytes length
      if( length8 > Integer.MAX_VALUE )
      throw new IOException("too big frame length");
      length = (int)length8;

instead of your method: byteArrayToLong()

Socket not connected

How to fix bugs?
java.lang.IllegalStateException: Socket not connected
at com.example.WebSocketClient$3.run(WebSocketClient.java:239)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.os.HandlerThread.run(HandlerThread.java:61)

Calling disconnect throws exception

03-25 16:29:39.992: E/TAG(21060): java.net.SocketException: Socket closed
03-25 16:29:39.992: E/TAG(21060): at libcore.io.Posix.recvfromBytes(Native Method)
03-25 16:29:39.992: E/TAG(21060): at libcore.io.Posix.recvfrom(Posix.java:131)
03-25 16:29:39.992: E/TAG(21060): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
03-25 16:29:39.992: E/TAG(21060): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
03-25 16:29:39.992: E/TAG(21060): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
03-25 16:29:39.992: E/TAG(21060): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-25 16:29:39.992: E/TAG(21060): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-25 16:29:39.992: E/TAG(21060): at libcore.io.Streams.readSingleByte(Streams.java:41)
03-25 16:29:39.992: E/TAG(21060): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:236)
03-25 16:29:39.992: E/TAG(21060): at java.io.DataInputStream.readByte(DataInputStream.java:96)
03-25 16:29:39.992: E/TAG(21060): at com.codebutler.android_websockets.HybiParser.start(HybiParser.java:112)
03-25 16:29:39.992: E/TAG(21060): at com.codebutler.android_websockets.WebSocketClient$1.run(WebSocketClient.java:134)
03-25 16:29:39.992: E/TAG(21060): at java.lang.Thread.run(Thread.java:856)
03-25 16:29:40.397: A/libc(21060): Fatal signal 11 (SIGSEGV) at 0x00000027 (code=1), thread 21065 (JDWP)

Readme Usage sample bug

The sample code shows specifying a new WebSocketClient.Handler in the constructor and this isn't defined in WebSocketClient.

This should be correct to:

 new WebSocketClient.Listener()

multi client

When I use multi client to multi port It will disconnected . for example when I use two clients to connect to two ports to send two files Simultaneouslly the first one will disconnect

Thread problems with Fragments

I know this has more to do with Koush's Socket.IO fork but I feel like it may apply to both.
I've posted the issue on SO and I feel like I explained it fairly well; Basically whenever I try to disconnect a socket inside a fragment, I get an error from the Looper.

http://stackoverflow.com/questions/14362387/android-socket-io-with-fragments

I'm more than happy to include any more information as this is really starting to drive me crazy trying to find a solution.

Thanks!

Client not seeing the "Sec-WebSocket-Accept" header

My server was sending the header as "Sec-Websocket-Accept" and not "Sec-WebSocket-Accept" (notice the capital "s"). The solution is change this line in "WebSocketClient":

if (header.getName().equals("Sec-WebSocket-Accept")) {

to this

if (header.getName().toLowerCase().equals("sec-websocket-accept")) {

According to the HTTP header standards: "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive."

NullPointer

05-19 12:30:01.670: E/AndroidRuntime(9231): FATAL EXCEPTION: websocket-thread
05-19 12:30:01.670: E/AndroidRuntime(9231): Process: com.mywebsockettest, PID: 9231
05-19 12:30:01.670: E/AndroidRuntime(9231): java.lang.NullPointerException
05-19 12:30:01.670: E/AndroidRuntime(9231): at com.codebutler.android_websockets.WebSocketClient$3.run(WebSocketClient.java:245)
05-19 12:30:01.670: E/AndroidRuntime(9231): at android.os.Handler.handleCallback(Handler.java:733)
05-19 12:30:01.670: E/AndroidRuntime(9231): at android.os.Handler.dispatchMessage(Handler.java:95)
05-19 12:30:01.670: E/AndroidRuntime(9231): at android.os.Looper.loop(Looper.java:136)
05-19 12:30:01.670: E/AndroidRuntime(9231): at android.os.HandlerThread.run(HandlerThread.java:61)

SSL Support

Do we have SSL support? In corporate environments, which will so have millions of Nexus7's behind the proxy, SSL will be need to get around the proxy server. Google did a big study on this a while back.

"Expected non-final packet" on fragmented messages

I'm having a little trouble receiving big messages from a node.js websocket server (if it's of any use, the server side uses this library). The connection seems to close with the "Expected non-final packet" exception every time a text message that can't be contained in a single frame is received.

I've took some time and read the code and the ietf rfc (this version here) and from what i see, it seems your code is choking on the initial frame that is supposed to start the fragmented message because it believes the message to be a continuation frame.

I've changed the list of opcodes allowed for fragmentation (FRAGMENTED_OPCODES) to only contain OP_CONTINUATION and it seems to work now. i am however unsure wether this is the correct fix for the situation at hand, so i'd like to hear your opinions on that.

connect to device with 4g active

Hello,

I would like to connect to server socket without needing to disable 4g.
I connect to the hotspot server socket and I try to connect but I have a time out.
When I disable the 4g I'm able to connect.

How to do to connect to the server without disabling 4g ?

Thanks

It says connected but it didnot

I used this in my android application, and I could see the function onConnect() get called, which is supposed to mean the websocket is connected. But from websocket server log it never connects. I am using WSS, CA certificate.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.