Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active August 27, 2018 11:46
Show Gist options
  • Save auycro/3b00e3c05b26d284ec77 to your computer and use it in GitHub Desktop.
Save auycro/3b00e3c05b26d284ec77 to your computer and use it in GitHub Desktop.
//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);
@Reelix
Copy link

Reelix commented Aug 22, 2018

The blog post was removed :(

@auycro
Copy link
Author

auycro commented Aug 27, 2018

@Reelix I already fixed it! thank for comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment