Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bodil/purescript-signal-socket
Node sockets with a Signal interface
https://github.com/bodil/purescript-signal-socket
Last synced: 23 days ago
JSON representation
Node sockets with a Signal interface
- Host: GitHub
- URL: https://github.com/bodil/purescript-signal-socket
- Owner: bodil
- License: lgpl-3.0
- Created: 2016-04-18T00:20:40.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-06-13T20:42:29.000Z (over 7 years ago)
- Last Synced: 2024-11-14T10:06:32.552Z (about 1 month ago)
- Language: PureScript
- Size: 23.4 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
# purescript-signal-socket
Node sockets with a Signal interface.
## Documentation
Read the [API documentation](https://pursuit.purescript.org/packages/purescript-signal-socket/).
## Usage
Connect to a socket and subscribe to a signal of socket events:
```purescript
import Signal.Socket as Socketmain = do
socket <- Socket.connect "localhost" 31337
eventSignal <- Socket.subscribe socket
```The signal is of type `Signal (Socket.Event String)`, and the
`Socket.Event` datatype looks like this:```purescript
import Control.Monad.Eff.Exception (Error)data Event a = Connecting
| Connected
| Drain
| Closed
| Timeout
| Data a
| Error Error
```To process the event stream:
```purescript
Signal.runSignal $ eventSignal ~> onSocketEvent
where onSocketEvent Connected = Socket.write socket "hai lol!"
onSocketEvent Timeout = Socket.end socket
onSocketEvent (Data s) = Socket.write socket $ "hi " ++ s
-- etc
```You can also get a signal of just the data from the socket (of type
`Signal (Maybe String)`, which will start at `Nothing`, yield `Just`s
of each data packet, then go back to `Nothing` when the socket
closes):```purescript
let dataSignal = S.onlyData eventSignal
```If you prefer to receive lines, you can split the data stream on a
given string, eg. newlines - this will also buffer output for you
until a complete line has been received:```purescript
let lines = S.split "\r\n" dataSignal
```## Licence
Copyright 2016 Bodil Stokke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
.