Skip to content

Commit

Permalink
Merge pull request #3 from zudov/new-api
Browse files Browse the repository at this point in the history
New api
  • Loading branch information
zudov committed Nov 17, 2015
2 parents 338ed7a + 11e3bee commit 34fc548
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 152 deletions.
28 changes: 28 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
language: node_js
sudo: false
node_js:
- 0.10
env:
- PATH=$HOME/purescript:$PATH
install:
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
- chmod a+x $HOME/purescript
- npm install bower pulp -g
- bower install
script:
- pulp build && pulp docs
- bower link
- cd example/ && bower link purescript-websocket-simple && bower install && pulp build

after_success:
- >-
test $TRAVIS_TAG &&
npm run psc-publish \
| tail -n 1 \
> output/documentation.json &&
curl -X POST http://pursuit.purescript.org/packages \
-d @output/documentation.json \
-H 'Accept: application/json' \
-H "Authorization: token ${GITHUB_TOKEN}"
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# Simple Bindings to Websocket API for Purescript

## Example usage
[![Documentation](http://pursuit.purescript.org/packages/purescript-websocket-simple/badge)](http://pursuit.purescript.org/packages/purescript-websocket-simple)
[![Latest release](http://img.shields.io/bower/v/purescript-websocket-simple.svg)](https://github.com/zudov/purescript-websocket-simple/releases)
[![Build Status](https://travis-ci.org/zudov/purescript-websocket-simple.svg?branch=master)](https://travis-ci.org/zudov/purescript-websocket-simple)

```haskell
module Main where
## Example usage

import Prelude
import Control.Monad.Eff.Console
import WebSocket
See `example/src/Main.purs`.

main = do
ws <- mkWebSocket "ws://echo.websocket.org"
onMessage ws log
onOpen ws $ do
send ws "hello"
send ws "world"
```
## Documentation

[Docs are on Pursuit](http://pursuit.purescript.org/packages/purescript-websocket-simple)
21 changes: 15 additions & 6 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "purescript-websocket-plain",
"version": "1.0.0",
"name": "purescript-websocket-simple",
"description": "A low-level wrapper around WebSocket API",
"authors": [
"Konstantin Zudov <co@zudov.me>"
],
"license": "BSD3",
"moduleType": [
"node"
],
Expand All @@ -10,13 +14,18 @@
"bower_components",
"output"
],
"repository": {
"type": "git",
"repository": {
"type": "git",
"url": "git://github.com/zudov/purescript-websocket-simple.git"
},
"keywords": [
"purescript"
],
"dependencies": {
"purescript-eff": "~0.1.0",
"purescript-functions": "~0.1.0"
"purescript-generics": "~0.6.2",
"purescript-eff": "~0.1.2",
"purescript-var": "~0.0.2",
"purescript-dom": "~0.2.8"
},
"devDependencies": {
"purescript-console": "~0.1.0"
Expand Down
170 changes: 141 additions & 29 deletions docs/WebSocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,196 @@

This module defines a simple low-level interface to the websockets API.

#### `WebSocket`
#### `WEBSOCKET`

``` purescript
data WebSocket :: !
data WEBSOCKET :: !
```

The effect associated with websocket connections.

#### `Socket`
#### `WebSocket`

``` purescript
data Socket :: *
data WebSocket :: *
```

A reference to a websocket.
A reference to a WebSocket object.

#### `URI`
#### `newWebSocket`

``` purescript
type URI = String
newWebSocket :: forall eff. URL -> Array Protocol -> Eff (ws :: WEBSOCKET | eff) Connection
```

A synonym for URI strings.
Initiate a websocket connection.

#### `Message`
#### `runMessageEvent`

``` purescript
type Message = String
runMessageEvent :: MessageEvent -> Message
```

A synonym for message strings.
#### `Connection`

#### `mkWebSocket`
``` purescript
newtype Connection
= Connection { binaryType :: forall eff. Var (ws :: WEBSOCKET | eff) BinaryType, bufferedAmount :: forall eff. GettableVar (ws :: WEBSOCKET | eff) BufferedAmount, onclose :: forall eff handlerEff. SettableVar (ws :: WEBSOCKET | eff) (CloseEvent -> Eff handlerEff Unit), onerror :: forall eff handlerEff. SettableVar (ws :: WEBSOCKET | eff) (Event -> Eff handlerEff Unit), onmessage :: forall eff handlerEff. SettableVar (ws :: WEBSOCKET | eff) (MessageEvent -> Eff handlerEff Unit), onopen :: forall eff handlerEff. SettableVar (ws :: WEBSOCKET | eff) (Event -> Eff handlerEff Unit), protocol :: forall eff. Var (ws :: WEBSOCKET | eff) Protocol, readyState :: forall eff. GettableVar (ws :: WEBSOCKET | eff) ReadyState, url :: forall eff. GettableVar (ws :: WEBSOCKET | eff) URL, close :: forall eff. Maybe Code -> Maybe Reason -> Eff (ws :: WEBSOCKET | eff) Unit, send :: forall eff. Message -> Eff (ws :: WEBSOCKET | eff) Unit, socket :: forall eff. GettableVar (ws :: WEBSOCKET | eff) WebSocket }
```

#### `BinaryType`

``` purescript
mkWebSocket :: forall e. URI -> Eff (ws :: WebSocket | e) Socket
data BinaryType
= Blob
| ArrayBuffer
```

Create a websocket object for a URI.
The type of binary data being transmitted by the connection.

#### `onMessage`
#### `BufferedAmount`

``` purescript
onMessage :: forall e a. Socket -> (Message -> Eff (ws :: WebSocket | e) a) -> Eff (ws :: WebSocket | e) Unit
newtype BufferedAmount
```

Register a callback for incoming messages.
The number of bytes of data that have been buffered (queued but not yet transmitted)

##### Instances
``` purescript
Generic BufferedAmount
Eq BufferedAmount
Ord BufferedAmount
```

#### `onError`
#### `runBufferedAmount`

``` purescript
onError :: forall e a. Socket -> Eff (ws :: WebSocket | e) a -> Eff (ws :: WebSocket | e) Unit
runBufferedAmount :: BufferedAmount -> Int
```

Register a callback for `error` events.
#### `Protocol`

``` purescript
newtype Protocol
= Protocol String
```

#### `onOpen`
A string indicating the name of the sub-protocol.

##### Instances
``` purescript
onOpen :: forall e a. Socket -> Eff (ws :: WebSocket | e) a -> Eff (ws :: WebSocket | e) Unit
Generic Protocol
Eq Protocol
Ord Protocol
```

Register a callback for `open` events.
#### `runProtocol`

``` purescript
runProtocol :: Protocol -> String
```

#### `onClose`
#### `ReadyState`

``` purescript
onClose :: forall e a. Socket -> Eff (ws :: WebSocket | e) a -> Eff (ws :: WebSocket | e) Unit
data ReadyState
= Connecting
| Open
| Closing
| Closed
```

Register a callback for `close` events.
State of the connection.

##### Instances
``` purescript
Generic ReadyState
Eq ReadyState
Ord ReadyState
Show ReadyState
Bounded ReadyState
Enum ReadyState
```

#### `Code`

``` purescript
newtype Code
= Code Int
```

A numeric value indicating the status code explaining why the connection is being closed.
See [the list of status codes](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes).

##### Instances
``` purescript
Generic Code
Eq Code
Ord Code
```

#### `runCode`

``` purescript
runCode :: Code -> Int
```

#### `send`
#### `Reason`

``` purescript
send :: forall e. Socket -> Message -> Eff (ws :: WebSocket | e) Unit
newtype Reason
= Reason String
```

Send a message to a websocket.
A human-readable string explaining why the connection is closing. This
string must be no longer than 123 bytes of UTF-8 text (not characters).

##### Instances
``` purescript
Generic Reason
Generic Reason
```

#### `runReason`

``` purescript
runReason :: Reason -> String
```

#### `URL`

``` purescript
newtype URL
= URL String
```

A synonym for URL strings.

##### Instances
``` purescript
Generic URL
```

#### `runURL`

``` purescript
runURL :: URL -> String
```

#### `Message`

``` purescript
newtype Message
= Message String
```

A synonym for message strings.

#### `runMessage`

``` purescript
runMessage :: Message -> String
```


17 changes: 17 additions & 0 deletions example/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "example",
"version": "1.0.0",
"moduleType": [
"node"
],
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"output"
],
"dependencies": {
"purescript-console": "^0.1.0",
"purescript-websocket-simple": "*"
}
}
2 changes: 2 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script src="output/script.js"></script>

41 changes: 41 additions & 0 deletions example/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Main where

import Prelude

import Control.Bind ((=<<))
import Control.Monad (when)
import Control.Monad.Eff.Var (($=), get)
import Control.Monad.Eff.Console (log)
import Control.Monad.Eff.Console.Unsafe (logAny)
import Data.Maybe (Maybe(..))

import WebSocket

main = do
Connection socket <- newWebSocket (URL "ws://echo.websocket.org") []

socket.onopen $= \event -> do
logAny event
log "onopen: Connection opened"

log <<< runURL =<< get socket.url

log "onopen: Sending 'hello'"
socket.send (Message "hello")

log "onopen: Sending 'goodbye'"
socket.send (Message "goodbye")

socket.onmessage $= \event -> do
logAny event
let received = runMessage (runMessageEvent event)

log $ "onmessage: Received '" ++ received ++ "'"

when (received == "goodbye") do
log "onmessage: closing connection"
socket.close Nothing Nothing

socket.onclose $= \event -> do
logAny event
log "onclose: Connection closed"
Loading

0 comments on commit 34fc548

Please sign in to comment.