Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jish/websocket-lab
A lab for experimenting with websockets
https://github.com/jish/websocket-lab
Last synced: 4 days ago
JSON representation
A lab for experimenting with websockets
- Host: GitHub
- URL: https://github.com/jish/websocket-lab
- Owner: jish
- Created: 2015-03-09T17:32:27.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-03-09T18:17:31.000Z (almost 10 years ago)
- Last Synced: 2024-10-31T17:13:51.264Z (about 2 months ago)
- Language: HTML
- Size: 117 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## Build a websocket echo server
Go to http://websocketd.com/, scroll all the way down to the bottom of the page, and download `websocketd`.
### Write a simple echo server
`websocketd` writes to your program on standard input and reads whatever you write out on standard output.
Let's write a simple Ruby script to echo back the message it received:
```ruby
#!/usr/bin/env rubywhile message = STDIN.gets
puts "Echo: #{message}"
STDOUT.flush
end
```### Create an HTML page to interact with your websocket server
The `ws` protocol, just like `http` can be transfered over a secure channel, (`wss` or `https`) or an insecure channel. Since we're just creating a test server, we're going to use the `ws://` protocol. In production you'll want to use `wss://`.
Let's use this example HTML page:
```html
Websocket test
Websocket test
var ws = new WebSocket('ws://localhost:8080/');ws.onmessage = function(event) {
console.log('Message from server: ' + event.data);
};ws.onopen = function() {
ws.send('Hello World!');
}
```
### Start it up!
Let's start our websocket server with the following command:
websocketd --staticdir=. --port=8080 ./echo_server.rb
Now open your browser to http://localhost:8080/ and interact with your server in the developer console:
```js
ws.send('Hello World!');
```