Skip to content

Instantly share code, notes, and snippets.

@otmb
Last active December 4, 2018 09:48
Show Gist options
  • Save otmb/9ad93d1209719731bd60609b9db62c87 to your computer and use it in GitHub Desktop.
Save otmb/9ad93d1209719731bd60609b9db62c87 to your computer and use it in GitHub Desktop.
Jetfire Unity Native
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using UnityEngine;
using AOT;
namespace UnityEngine.XR.iOS
{
public class Jetfire {
public static Queue<byte[]> ByteQueue = new Queue<byte[]>();
delegate void JetfireConnectCallback();
[MonoPInvokeCallbackAttribute(typeof(JetfireConnectCallback))]
private static void ConnectCallback (){
Debug.Log("connect");
}
delegate void JetfireDisConnectCallback(string message);
[MonoPInvokeCallbackAttribute(typeof(JetfireDisConnectCallback))]
private static void DisConnectCallback(string message){
Debug.Log("dis connect: " + message);
}
delegate void JetfireReceiveMessageCallback(string message);
[MonoPInvokeCallback(typeof(JetfireReceiveMessageCallback))]
private static void ReceiveMessageCallback(string message) {
Debug.Log("msg: " + message);
}
delegate void JetfireReceiveDataCallback(IntPtr pnt,ulong size);
[MonoPInvokeCallback(typeof(JetfireReceiveDataCallback))]
private static void ReceiveDataCallback(IntPtr pnt, ulong size) {
if (ByteQueue.Count < 1){
byte[] bytes = new byte[size];
Marshal.Copy(pnt, bytes, 0, (int)size);
ByteQueue.Enqueue(bytes);
}
}
#if UNITY_IOS && !UNITY_EDITOR
private const string DllName = "__Internal";
#else
private const string DllName = "libJetfire";
#endif
// [DllImport("__Internal")]
// private static extern void JetfireConnect(JetfireCallBackDelegate callback,string path);
[DllImport(DllName)]
private static extern void JetfireOpen(
string path,
JetfireConnectCallback _connectCallback,
JetfireDisConnectCallback _disConnectCallback,
JetfireReceiveMessageCallback _receiveMessageCallback,
JetfireReceiveDataCallback _receiveDataCallback
);
[DllImport(DllName)]
private static extern void JetfireConnect();
[DllImport(DllName)]
private static extern void JetfireClose();
[DllImport(DllName)]
private static extern void JetfirePing();
[DllImport(DllName)]
private static extern void JetfireSendMsg(string msg);
[DllImport(DllName)]
private static extern void JetfireSendData(byte[] bytes,int size);
[DllImport(DllName)]
private static extern bool JetfireIsConnected();
public static void Open(string path) {
JetfireOpen(path,
ConnectCallback,
DisConnectCallback,
ReceiveMessageCallback,
ReceiveDataCallback);
}
public static void Connect() {
JetfireConnect();
}
public static void Close() {
JetfireClose();
}
public static void Ping() {
JetfirePing();
}
public static void SendMsg(string msg) {
JetfireSendMsg(msg);
}
public static void SendData(byte[] bytes) {
JetfireSendData(bytes,bytes.Length);
}
public static bool IsConnected() {
return JetfireIsConnected();
}
}
}
#import "JFRWebSocket.h"
typedef void (*UNITY_CONNECT_CALLBACK)();
typedef void (*UNITY_DISCONNECT_CALLBACK)(char*);
typedef void (*UNITY_RECEIVEMESSAGE_CALLBACK)(char*);
typedef void (*UNITY_RECEIVEDATA_CALLBACK)(void*,unsigned long);
@interface UnityJetfire : NSObject <JFRWebSocketDelegate>
{
@public
UNITY_CONNECT_CALLBACK _connectCallback;
UNITY_DISCONNECT_CALLBACK _disConnectCallback;
UNITY_RECEIVEMESSAGE_CALLBACK _receiveMessageCallback;
UNITY_RECEIVEDATA_CALLBACK _receiveDataCallback;
}
@property(nonatomic, strong)JFRWebSocket *socket;
@end
@implementation UnityJetfire
-(void)websocketDidConnect:(JFRWebSocket*)socket {
NSLog(@"websocket is connected");
_connectCallback();
}
-(void)websocketDidDisconnect:(JFRWebSocket*)socket error:(NSError*)error {
NSLog(@"websocket is disconnected: %@", [error localizedDescription]);
//[self.socket connect];
NSString* string = [error localizedDescription];
_disConnectCallback((char *) [string UTF8String]);
}
-(void)websocket:(JFRWebSocket*)socket didReceiveMessage:(NSString*)string {
NSLog(@"Received text: %@", string);
_receiveMessageCallback((char *) [string UTF8String]);
}
-(void)websocket:(JFRWebSocket*)socket didReceiveData:(NSData*)data {
// NSLog(@"Received data: %@", data);
NSUInteger len = [data length];
NSLog(@"websocket dataSize: %u",len);
if (len < 1048576){
// Byte *byteData = (Byte*)malloc(len);
// memcpy(byteData, [data bytes], len);
unsigned char *byteData = (unsigned char *)[data bytes];
_receiveDataCallback(byteData,len);
//free(byteData);
}
data = nil;
}
@end
extern "C" {
UnityJetfire *client;
void JetfireOpen(
const char* _path,
UNITY_CONNECT_CALLBACK _connectCallback,
UNITY_DISCONNECT_CALLBACK _disConnectCallback,
UNITY_RECEIVEMESSAGE_CALLBACK _receiveMessageCallback,
UNITY_RECEIVEDATA_CALLBACK _receiveDataCallback)
{
client = [[UnityJetfire alloc] init];
client->_connectCallback = _connectCallback;
client->_disConnectCallback = _disConnectCallback;
client->_receiveMessageCallback = _receiveMessageCallback;
client->_receiveDataCallback = _receiveDataCallback;
NSString *path = [NSString stringWithUTF8String:_path];
NSLog(@"connect: %@",path);
client.socket = [[JFRWebSocket alloc] initWithURL:[NSURL URLWithString:path] protocols:@[@"chat",@"superchat"]];
client.socket.delegate = client;
[client.socket connect];
}
void JetfireConnect(){
[client.socket connect];
}
void JetfireClose(){
[client.socket disconnect];
}
void JetfirePing(){
uint8_t byte = 1;
NSData *data = [NSData dataWithBytes:&byte length:1];
[client.socket writePing: data];
}
void JetfireSendMsg(const char * msg){
[client.socket writeString: [NSString stringWithUTF8String:msg]];
}
void JetfireSendData(const void * bytes,int size){
//NSLog(@"SendData %d",size);
NSData *data = [NSData dataWithBytes:(const void *)bytes length:(sizeof(uint8_t) * size)];
[client.socket writeData: data];
}
bool JetfireIsConnected(){
// NSLog(@"is connection: %d", client.socket.isConnected);
return client.socket.isConnected;
}
}
@otmb
Copy link
Author

otmb commented Aug 24, 2018

cp "${TARGET_BUILD_DIR}/libJetfire-Static.a" \
"${PROJECT_DIR}/../Assets/Plugins/iOS/Jetfire/libJetfire.a"

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