Skip to content

Instantly share code, notes, and snippets.

@RblSb
Created March 3, 2018 03:15
Show Gist options
  • Save RblSb/97806c52d1cb4ff23fd87e72cffe5fb3 to your computer and use it in GitHub Desktop.
Save RblSb/97806c52d1cb4ff23fd87e72cffe5fb3 to your computer and use it in GitHub Desktop.
hxbit websockethost
/*
* Copyright (C)2015-2016 Nicolas Cannasse
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package hxd.net;
#if !hxbit
#error "Using WebSocketHost requires compiling with -lib hxbit"
#end
import hxbit.NetworkHost;
import js.html.WebSocket in Socket;
import haxe.Constraints.Function;
class SocketClient extends NetworkClient {
var socket : Socket;
public function new(host, s) {
super(host);
this.socket = s;
if( s != null )
s.onmessage = function(e:{data:js.html.Uint8Array}) {
// process all pending messages
/*while( socket != null && readData(e.data, socket.input.available) ) {
}*/
var data = haxe.io.Bytes.ofData(cast e.data);
var input = new haxe.io.BytesInput(data);
try {
while(readData(input, e.data.byteLength) ) {}
} catch (err:Dynamic) { //haxe.io.Eof
//trace("error" + err);
}
}
}
override function error(msg:String) {
socket.close();
super.error(msg);
}
override function send( bytes : haxe.io.Bytes ) {
/*socket.out.wait();
socket.out.writeInt32(bytes.length);
socket.out.write(bytes);
socket.out.flush();*/
var len = bytes.length;
var b = haxe.io.Bytes.alloc(4 + len);
b.setInt32(0, len);
b.blit(4, bytes, 0, len);
socket.send(b.getData());
}
override function stop() {
super.stop();
if( socket != null ) {
socket.close();
socket = null;
}
}
}
@:jsRequire("ws", "Server")
extern class WSServer {
function new(obj:{port:Int});
function on(type:String, func:Function):String;
}
class SocketHost extends NetworkHost {
var connected = false;
var socket : Socket;
public var enableSound : Bool = true;
public function new() {
super();
isAuth = false;
}
override function dispose() {
super.dispose();
close();
}
function close() {
if( socket != null ) {
socket.close();
socket = null;
}
connected = false;
}
public function connect( host : String, port : Int, ?onConnect : Bool -> Void ) {
close();
isAuth = false;
/*socket = new Socket();
socket.onError = function(msg) {
if( !connected ) {
socket.onError = function(_) { };
onConnect(false);
} else
throw msg;
};
self = new SocketClient(this, socket);
socket.connect(host, port, function() {
connected = true;
if( host == "127.0.0.1" ) enableSound = false;
clients = [self];
onConnect(true);
});*/
socket = new Socket("ws://" + host + ":" + port);
socket.binaryType = ARRAYBUFFER; //BLOB //ARRAYBUFFER
socket.onerror = function(msg) {
if (!connected) {
socket.onerror = function(_) {};
onConnect(false);
} else throw msg;
}
self = new SocketClient(this, socket);
socket.onopen = function() {
connected = true;
if (host == "127.0.0.1") enableSound = false;
clients = [self];
onConnect(true);
}
}
public function wait( host : String, port : Int, ?onConnected : NetworkClient -> Void ) {
#if !nodejs
throw ("wait unsupported");
#end
close();
isAuth = false;
/*socket = new Socket();
self = new SocketClient(this, null);
socket.bind(host, port, function(s) {
var c = new SocketClient(this, s);
pendingClients.push(c);
s.onError = function(_) c.stop();
if( onConnected != null ) onConnected(c);
});*/
self = new SocketClient(this, null);
var wss = new WSServer({port: port});
wss.on("connection", function(ws) {
var c = new SocketClient(this, ws);
pendingClients.push(c);
ws.onerror = function(e) {
c.stop();
}
if (onConnected != null) onConnected(c);
});
isAuth = true;
}
public function offlineServer() {
close();
self = new SocketClient(this, null);
isAuth = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment