Created
April 5, 2013 10:25
-
-
Save jasononeil/5318246 to your computer and use it in GitHub Desktop.
A haxe port of the WebSockets demo here: http://www.websocket.org/echo.html Works with Haxe3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8" /> | |
<title>WebSocket Test</title> | |
<script language="javascript" type="text/javascript" src="WebSocketTest.js"></script> | |
<h2>WebSocket Test</h2> | |
<button type="submit" id="open">Open</button> | |
<button type="submit" id="close">Close</button> | |
<input type="text" id="input" /> | |
<button type="submit" id="send">Send</button> | |
<div id="output"></div> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with: haxe -main WebSocketTest.hx -js WebSocketTest.js | |
// Doing your imports like this makes it much easier to port with existing Javascript code. | |
import js.Browser.window; | |
import js.Browser.document; | |
import js.html.*; | |
class WebSocketTest | |
{ | |
static var wsUri = "ws://echo.websocket.org/"; | |
static var output:Element; | |
static var websocket:WebSocket; | |
static function main() | |
{ | |
window.addEventListener("load", init, false); | |
} | |
static function init(e) | |
{ | |
output = document.getElementById("output"); | |
var input:InputElement = cast document.getElementById("input"); | |
var openBtn = document.getElementById("open"); | |
var sendBtn = document.getElementById("send"); | |
var closeBtn = document.getElementById("close"); | |
// Get the buttons to do things | |
openBtn.addEventListener("click", connect, false); | |
sendBtn.addEventListener("click", function (e) { | |
doSend(input.value); | |
}, false); | |
closeBtn.addEventListener("click", function (e) { | |
websocket.close(); | |
}, false); | |
} | |
static function connect(evt) | |
{ | |
websocket = new WebSocket(wsUri); | |
websocket.onopen = onOpen; | |
websocket.onclose = onClose; | |
// Haxe seems to have some bad type definitions here. If you're willing to investigate, I'm sure | |
// filing an issue at http://code.google.com/p/haxe/issues/list would see a fix in time for 3.0 :) | |
websocket.onmessage = cast onMessage; | |
websocket.onerror = cast onError; | |
} | |
static function onOpen(evt) | |
{ | |
writeToScreen("CONNECTED"); | |
doSend("WebSocket rocks"); | |
} | |
static function onClose(evt) | |
{ | |
writeToScreen("DISCONNECTED"); | |
} | |
static function onMessage(evt:MessageEvent) | |
{ | |
writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>'); | |
} | |
static function onError(evt:MessageEvent) | |
{ | |
writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data); | |
} | |
static function doSend(message) | |
{ | |
writeToScreen("SENT: " + message); | |
websocket.send(message); | |
} | |
static function writeToScreen(message) | |
{ | |
var pre = document.createElement("p"); | |
pre.style.wordWrap = "break-word"; | |
pre.innerHTML = message; | |
output.appendChild(pre); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment