Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback sample code block in C# #4

Closed
jafarulla15 opened this issue Jun 3, 2020 · 4 comments
Closed

Callback sample code block in C# #4

jafarulla15 opened this issue Jun 3, 2020 · 4 comments
Labels

Comments

@jafarulla15
Copy link

Hello, I am new in ice and mumble.

Can you give me a sample code for callback. I don't found any example in C#.
It will be a very helpful for me and will save my time, as I already spend 3/4 days after it and don't found any solution in C#.

Best Regards

Jafar Ulla

@HarpyWar
Copy link
Owner

HarpyWar commented Jun 3, 2020

var address = "x.x.x.x";
var port = 6500; // ice port
var secret = "icewritepassword";
var callbackAddress = "127.0.0.1"; // must be 127.0.0.1

#region ADAPTER INSTANCE
// a) use adapter
//var version = "1.2.4";
//instance = new MurmurAdapter.Adapter(version).Instance;
#endregion
#region DIRECT INSTANCE
// b) or use direct connection to specified version
instance = new Murmur.Instance();
#endregion

instance.Connect(address, port, secret, callbackAddress);

// callbacks for the instance (events for stop/start virtual servers)
var icb = new InstanceCallbackHandler();
icb_id = instance.AddCallback(icb);


// select virtual server by id
var s = instance.GetServer(1);
// callbacks for a virtual server
var vcb = new VirtualServerCallbackHandler();
// attach callbacks
var vcb_id = s.AddCallback(vcb);


// do it for all servers
//var servers = instance.GetAllServers();
//foreach (var s in servers)
//{
//	if (!s.IsRunning())
//		continue;
//	var cb = new VirtualServerCallbackHandler();
//	var cb_id = s.AddCallback(vcb);
//	... save callback ids somewhere to delete later
//}

...
// remove any callback if needed
s.RemoveCallback(vcb_id);
instance.RemoveCallback(icb_id);
class InstanceCallbackHandler : IInstanceCallbackHandler
{
	public void Started(IVirtualServer server)
	{
		Console.WriteLine("server {0} started", server.Id);
	}
	public void Stopped(IVirtualServer server)
	{
		Console.WriteLine("server {0} stopped", server.Id);
	}
}

class VirtualServerCallbackHandler : IVirtualServerCallbackHandler
{
	public void ChannelCreated(VirtualServerEntity.Channel channel, IVirtualServer server)
	{
		Console.WriteLine("channel {0} created", channel.Name);
	}

	public void ChannelRemoved(VirtualServerEntity.Channel channel, IVirtualServer server)
	{
		Console.WriteLine("channel {0} removed", channel.Name);
	}

	public void ChannelStateChanged(VirtualServerEntity.Channel channel, IVirtualServer server)
	{
		Console.WriteLine("channel {0} state changed", channel.Name);
	}

	public void UserConnected(VirtualServerEntity.OnlineUser user, IVirtualServer server)
	{
		Console.WriteLine("user {0} connected", user.Name);

		var ccb = new VirtualServerContextCallbackHandler();
		var cbid = server.AddContextCallback(user.Session, "superaction", "My Super Menu Item", ccb, Context.Server);
		// TODO: remove on user disconnect event
		//server.RemoveCallback(cbid);
	}

	public void UserDisconnected(VirtualServerEntity.OnlineUser user, IVirtualServer server)
	{
		Console.WriteLine("user {0} disconnected", user.Name);
	}

	public void UserStateChanged(VirtualServerEntity.OnlineUser user, IVirtualServer server)
	{
		Console.WriteLine("user {0} state changed", user.Name);
	}

	public void UserTextMessage(VirtualServerEntity.OnlineUser user, string message, IVirtualServer server)
	{
		Console.WriteLine("user {0} write message {1}", user.Name, message);
	}
}

class VirtualServerContextCallbackHandler : IVirtualServerContextCallbackHandler
{
	public void ContextAction(string action, VirtualServerEntity.OnlineUser user, int session, int channelId, IVirtualServer server)
	{
		Console.WriteLine("action from {0}, session {1}, channelId {2} on server {3}", user.Name, session, channelId, server.Id);
	}
}

Note about #1

@jafarulla15
Copy link
Author

Thanks a lot for so quick response. I have tried your code and I think it's correct.
But I don't get any response fire.
I don't know, is there anything I need to enable at mumble server to get callback functional.
I am sorry to say, but can you please take a kind look at my implementation. Here is a console C# project.

https://www.dropbox.com/s/0oh95d32lcqdtdw/Ice_CallBack_Test.zip?dl=0
Many many thanks
Jafar Ulla

@jafarulla15
Copy link
Author

I got the issue. It's a networking issue. Mumble at aws server can't find my local ice client pc.

It's working fine when mumble server and ice client in the same network (ip of same series) and I am giving callbackAddress = my local pc IP and mumble server also in my local network, then it's working fine.

But is there any way to use the mumbler server in real IP and ice client local IP.
What is the configuration I need to edit, please?

Thanks
Jafar Ulla

@HarpyWar
Copy link
Owner

HarpyWar commented Jun 4, 2020

If PublishedEndpoints used for Murmur then callback address may be different from localhost. You should try it, I didn't test such case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants