Last active
August 27, 2018 11:46
-
-
Save auycro/3b00e3c05b26d284ec77 to your computer and use it in GitHub Desktop.
WebSocketJsLib.jslib for Unity Tutorial (https://auycro.github.io/blog/2015/09/02/implement-websocket-unity-webgl-javascript-plugin1)
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
//Copyright (c) 2015 Gumpanat Keardkeawfa | |
//Licensed under the MIT license | |
//Websocket Jslib for UnityWebgl | |
//We will save this file as *.jslib for using in UNITY | |
var WebSocketJsLib = { | |
Hello: function(){ | |
window.alert("Hello,world!"); | |
}, | |
InitWebSocket: function(url){ | |
var init_url = Pointer_stringify(url); | |
window.wsclient = new WebSocket(init_url); | |
window.wsclient.onopen = function(evt){ | |
console.log("[open]"+init_url); | |
window.wsclient.send("hello"); | |
}; | |
window.wsclient.onclose = function(evt) { | |
console.log("[close] "+evt.code+":"+evt.reason); | |
}; | |
window.wsclient.onmessage = function(evt) { | |
var received_msg = evt.data; | |
if (received_msg == "hello") { | |
window.wsclient.send("hello"); | |
} else { | |
console.log("[recv] "+received_msg); | |
SendMessage('WebSocketGameObject', 'RecvString', received_msg); | |
} | |
}; | |
window.wsclient.onerror = function(evt) { | |
var error_msg = evt.data; | |
console.log("[error] "+error_msg); | |
SendMessage('WebSocketGameObject', 'ErrorString', "close"); | |
}; | |
}, | |
State: function(){ | |
var status = 0; | |
if ((typeof window.wsclient !== "undefined")&& (window.wsclient !== null)) | |
status = window.wsclient.readystate; | |
return status; | |
}, | |
Send: function(msg){ | |
var message = Pointer_stringify(msg); | |
if (typeof window.wsclient !== "undefined") { | |
console.log("[send] "+message); | |
window.wsclient.send(message); | |
} else { | |
console.log("[send-failed] "+message); | |
} | |
}, | |
Close: function(){ | |
if ((typeof window.wsclient !== "undefined")&& (window.wsclient !== null)) | |
window.wsclient.close(); | |
} | |
} | |
mergeInto(LibraryManager.library, WebSocketJsLib); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The blog post was removed :(