GithubHelp home page GithubHelp logo

igit-cn / websocket-scala-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andyglow/websocket-scala-client

0.0 1.0 0.0 40 KB

WebSocket client based on Netty

License: GNU Lesser General Public License v3.0

Scala 100.00%

websocket-scala-client's Introduction

Websocket Client for Scala

Build Status Maven Central Download

WebSocket client based on Netty

Usage

build.sbt

libraryDependencies += "com.github.andyglow" %% "websocket-scala-client" % ${LATEST_VERSION} % Compile

Code

Import

import com.github.andyglow.websocket._

Simple example

  // 1. prepare ws-client
  // 2. define message handler
  val cli = WebsocketClient[String]("ws://echo.websocket.org") {
    case str =>
      logger.info(s"<<| $str")
  }

  // 4. open websocket
  val ws = cli.open()

  // 5. send messages
  ws ! "hello"
  ws ! "world"

A bit more advanced example

  // define some mutex to be able to shutdown app when message passing ends 
  val semaphore = new Semaphore(0)
  
  // define our protocol handler
  val protocolHandler = new WebsocketHandler[String]() {
    def receive = {
      case str if str startsWith "repeat " =>
        sender() ! "repeating " + str.substring(7)
        logger.info(s"<<| $str")

      case str if str endsWith "close" =>
        logger.info(s"<<! $str")
        sender().close
        semaphore.release()

      case str =>
        logger.info(s"<<| $str")
    }
  }

  // create websocket client and open connection
  val cli = WebsocketClient(Uri("ws://echo.websocket.org"), protocolHandler)
  val ws = cli.open()

  // send some messages
  ws ! "hello"
  ws ! "world"
  ws ! "repeat and close"
  
  // wait for echoed 'repeating close'
  semaphore.acquire(1)
  
  // shutdown whole the netty stack
  cli.shutdownSync()

Defining websocket handler this way we are able to communicate back straight from handler. Use sender() for that.

For more examples please see Examples Section

To run examples one can use sbt

sbt> example:run

or

sbt> example:runMain [BinaryEchoWebSocketOrg|SimplifiedTextEchoWebSocketOrg|TextEchoWebSocketOrg]

websocket-scala-client's People

Contributors

1van3 avatar antonzherdev avatar dcheckoway avatar lenstr avatar synesso avatar

Watchers

 avatar

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.