Code Monkey home page Code Monkey logo

websocketclient's Introduction

WebSocketClient

Java Module for connecting to TCP-IP WebSockets without any other external dependencies. (this module uses javax.websocket module which is included in this package jar file)

Installation

Simply include the WebSocketClient.jar file in your project.

Special Guide For Android Projects:

  • Create libs folder in your app folder where your app level build.gradle file is present.
  • Download WebSocketClient.jar from here, and place it in the libs folder created in previous step.
  • In your app level build.gradle file, add the following line in dependencies block:
implementation fileTree(dir: "libs", include: ["*.jar"])
  • Done!

Usage

Simply use this code (Modify according to your needs):

import com.abhaytr.tools.*;

class YourClass
{
  
  public static void main(String args[])
  {
    WebSocketClient ws = new WebSocketClient("wss://your-url.com/ws", new MessageListener()    //Replace "wss://your-url.com/ws" with Client's Web Socket URL.
    {
        
	@Ovveride
	public void onOpen()
	{
	    //Called when connection is established between Web Socket Server and Client.
	}

	@Ovveride
	public void onMessage(String message)
	{
	    /*
	    Called when Web Socket Server sends a message to the client.
  	    "message" contains the message sent from the Web Socket Server.
	    */
	}

	@Ovveride
	public void onClose(String error)
	{
	    /*
	    Called when connection is disrupted/failed between Web Socket Server and Client.
  	    "error" is the reason for connection disruption/failiure.
	    */
	}

    });
    //Functions available for module specified below in "Functions" section.
  }

}

Functions

addMessageListener(MessageListener msgListener)

ws.addMessageListener(new MessageListener()
{
        
    @Ovveride
    public void onOpen()
    {
	//Called when connection is established between Web Socket Server and Client.
    }

    @Ovveride
    public void onMessage(String message)
    {
	/*
	Called when Web Socket Server sends a message to the client.
  	"message" contains the message sent from the Web Socket Server.
	*/
    }

    @Ovveride
    public void onClose(String error)
    {
	/*
	Called when connection is disrupted/failed between Web Socket Server and Client.
  	"error" is the reason for connection disruption/failiure.
	*/
    }

});

Attaches a listener (java interface) which handles events such as onOpen(), onClose(String error) and onMessage(String message).

sendMessage(String message)

ws.sendMessage("Hello Web Socket Server!");   //Replace "Hello Web Socket Server!" with Client's message. 

Sends Client's message to the Web Socket Server.

Useful for realtime connections using TCP-IP Web Sockets in Java.

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.