Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ianharrigan/hxWebSockets
hxWebSockets - websockets for all haxe platforms
https://github.com/ianharrigan/hxWebSockets
haxe websockets
Last synced: 6 days ago
JSON representation
hxWebSockets - websockets for all haxe platforms
- Host: GitHub
- URL: https://github.com/ianharrigan/hxWebSockets
- Owner: ianharrigan
- Created: 2018-12-06T14:25:53.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-18T17:03:35.000Z (4 months ago)
- Last Synced: 2024-08-02T07:08:17.105Z (3 months ago)
- Topics: haxe, websockets
- Language: Haxe
- Homepage: https://github.com/ianharrigan/hxWebSockets
- Size: 104 KB
- Stars: 80
- Watchers: 10
- Forks: 17
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-haxe-gamedev - hxWebSockets - Websockets for all platforms. (Networking)
README
# hxWebSockets
###### Fork of https://github.com/soywiz/haxe-wsHaxe Websockets supporting the following targets:
- C++
- HashLink
- JavaScriptIncomplete support for Java and C#.
# Examples
### Client
```haxe
import haxe.io.Bytes;
import hx.ws.Log;
import hx.ws.WebSocket;
class Main {
static function main() {
Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
var ws = new WebSocket("ws://localhost:5000");
ws.onopen = function() {
ws.send("alice string");
ws.send(Bytes.ofString("alice bytes"));
}
#if sys
Sys.getChar(true);
#end
}
}
```### Server
`Main.hx`
```haxe
import hx.ws.Log;
import hx.ws.WebSocketServer;
class Main {
static function main() {
Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
var server = new WebSocketServer("localhost", 5000, 10);
server.start();
}
}
````MyHandler.hx`
```haxe
import hx.ws.SocketImpl;
import hx.ws.WebSocketHandler;
import hx.ws.Types;
class MyHandler extends WebSocketHandler {
public function new(s: SocketImpl) {
super(s);
onopen = function() {
trace(id + ". OPEN");
}
onclose = function() {
trace(id + ". CLOSE");
}
onmessage = function(message: MessageType) {
switch (message) {
case BytesMessage(content):
trace(content.readAllAvailableBytes());
case StrMessage(content):
var str = "echo: " + content;
trace(str);
send(str);
}
}
onerror = function(error) {
trace(id + ". ERROR: " + error);
}
}
}
```### Secure server
`Main.hx`
```haxe
package;import hx.ws.WebSocketSecureServer;
import sys.ssl.Key;
import sys.ssl.Certificate;class Main
{public static function main()
{
// self signed ceritificate
var cert = Certificate.loadFile('example.cert');
var key = Key.loadFile('example.key');var server = new WebSocketSecureServer("0.0.0.0", 5000,
cert, // actual certificate
key, // key to the certificate
cert, // certificate chain to aid clients finding way to trusted root,
// pass cert in case of selfsigned
10);
server.start();
}
}
```Initialize client with `wss` protocol, e.g. `new WebSocket("wss://localhost:5000");`
### Accepting selfsigned certs
Only on sys platforms, since they expose SslSocket. If you need to test JS with selfsigned certs, you need to import certificate into your browser trusted collection.
```haxe
import hx.ws.Log;
import hx.ws.WebSocket;import hx.ws.SocketImpl;
import hx.ws.SecureSocketImpl;class WebSocketNoVerify extends WebSocket {
override private function createSocket():SocketImpl
{
if (_protocol == "wss") {
var socket:SecureSocketImpl = cast super.createSocket();
socket.verifyCert = false;
return socket;
}
return super.createSocket();
}
}class Main {
static function main() {
Log.mask = Log.INFO | Log.DEBUG | Log.DATA;
var ws = new WebSocketNoVerify("wss://localhost:5000");
ws.onopen = function() {
ws.send("alice string");
}
Sys.getChar(true);
}
}
```