websocket-scala-client


License

License

Categories

Categories

Scala Languages CLI User Interface
GroupId

GroupId

com.github.andyglow
ArtifactId

ArtifactId

websocket-scala-client_2.12
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

websocket-scala-client
websocket-scala-client
Project URL

Project URL

http://github.com/andyglow/websocket-scala-client
Project Organization

Project Organization

andyglow
Source Code Management

Source Code Management

https://github.com/andyglow/websocket-scala-client

Download websocket-scala-client_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.andyglow/websocket-scala-client_2.12/ -->
<dependency>
    <groupId>com.github.andyglow</groupId>
    <artifactId>websocket-scala-client_2.12</artifactId>
    <version>0.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.andyglow/websocket-scala-client_2.12/
implementation 'com.github.andyglow:websocket-scala-client_2.12:0.4.0'
// https://jarcasting.com/artifacts/com.github.andyglow/websocket-scala-client_2.12/
implementation ("com.github.andyglow:websocket-scala-client_2.12:0.4.0")
'com.github.andyglow:websocket-scala-client_2.12:jar:0.4.0'
<dependency org="com.github.andyglow" name="websocket-scala-client_2.12" rev="0.4.0">
  <artifact name="websocket-scala-client_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.andyglow', module='websocket-scala-client_2.12', version='0.4.0')
)
libraryDependencies += "com.github.andyglow" % "websocket-scala-client_2.12" % "0.4.0"
[com.github.andyglow/websocket-scala-client_2.12 "0.4.0"]

Dependencies

compile (5)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.12
io.netty : netty-all jar 4.1.63.Final
io.netty : netty-codec-http jar 4.1.63.Final
org.scala-stm : scala-stm_2.12 jar 0.11.1
org.slf4j : slf4j-api jar 1.7.30

test (3)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.2.9
com.typesafe.akka : akka-http_2.12 jar 10.2.4
com.typesafe.akka : akka-stream_2.12 jar 2.6.9

Project Modules

There are no modules declared in this project.

Websocket Client for Scala

Build Status mvn

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]

Versions

Version
0.4.0
0.3.0
0.2.4
0.2.0