Determine if player is the host or the client - networking

I want to assign specific information for the server's characters and as well as the client's characters. Now, how do I know if the player is the host or the client? I tried using isServer and isClient, but it both return true. Are these the correct keywords that I should use?
void Update () {
if(isServer){
Debug.Log("I'm the server");
}
if(isClient){
Debug.Log("I'm the client");
}
}

If you're connecting as a "host", you're actually both the "client" and "server" at the same time. This is in contrast to running a "dedicated server", which acts as the server authority, but doesn't represent a "client" connection. Like you suggest in your own answer, you can use isServer and !isServer, or probably:
void Update() {
if (isServer) {
Debug.Log("I'm the server (or host)");
} else {
Debug.Log("I'm the client");
}
}

Instead of using isClient to determine if player is the client, i use !isServer instead.
void Update () {
if(isServer){
Debug.Log("I'm the server");
}
if(!isServer){
Debug.Log("I'm the client");
}
}

Not sure if this applies to every situation, so I apologize if it does not - I am using a plugin called NATTraversal for Unity, and I was having a similar issue. I needed to find which connection is the host. However for me, since I am not using the relay servers (this is for you guys who are avoiding the relay) I found that I can do this check..
using UnityEngine.Networking;
void Start(){
if(NetworkServer.connections.Count > 0){
Debug.Log("This is the host.");
} else {
Debug.Log("This is a client.");
}
}
This works in my scenario because the client's connection list is empty, but the host's is not. There very well may be a better way to do this, but I didn't know of one without a previous built list of NetworkIdentity's.
The Network.isServer bool always returns false for me, so this is how I got around it. Hopefully it helps someone out there.
Edit: (Adding crucial information)
Please note, that this is AFTER matchmaking and connections have been established.
Another way to do it I have found is by listening to OnServerConnect in the NATLobbyManager.
public override void OnServerConnect(NetworkConnection conn){ }
That event only triggers for the host with the NATTraversal plugin, more info for anyone who may come across this while trying to figure all this stuff out. :)

Related

Cloudflare Workers - changes are not visible on live (but are in preview)

Hello and thank you for your help.
Sadly support over at CF does not think they need to help me.
I am learning to use workers, and have written a simple HTML injector just to see it working on my site.
this is the full worker code i have:
async function handleRequest(req) {
const res = await fetch(req)
const contentType = res.headers.get("Content-Type")
console.log('contentType: ', contentType)
// If the response is HTML, it can be transformed with
// HTMLRewriter -- otherwise, it should pass through
if (contentType.startsWith("text/html")) {
return rewriter.transform(res)
} else {
return res
}
}
class UserElementHandler {
async element(element) {
element.before("<div class='contbox'><img src='https://coverme.co.il/wp-content/uploads/2020/01/covermeLOGO-01-1024x183.png' style='width:200px;margin:20px;'><h1>testing inserting</h1></div>", {html: true});
// fill in user info using response
}
}
const rewriter = new HTMLRewriter()
.on("h1", new UserElementHandler())
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
it just uses element.before to inject some HTML.
in the worker preview pane i can see it!
but on the live site = nothing.
this is the active URL: [https://coverme.co.il/product/%D7%A0%D7%A8-%D7%91%D7%99%D7%A0%D7%95%D7%A0%D7%99-tuberosejasmine/]
these are the 4 routes i have set up to try to catch this, with and without encoding the letters:
coverme.co.il/product/נר-בינוני-tuberosejasmine/
*.coverme.co.il/product/נר-בינוני-tuberosejasmine/*
https://coverme.co.il/product/%D7%A0%D7%A8-%D7%91%D7%99%D7%A0%D7%95%D7%A0%D7%99-tuberosejasmine/
*.coverme.co.il/product/%D7%A0%D7%A8-%D7%91%D7%99%D7%A0%D7%95%D7%A0%D7%99-tuberosejasmine/*
thanks in advance!
I believe the problem here is that you've configured your routes to match "נר-בינוני" unescaped, but the browser will actually percent-encode the URL before sending to the server, therefore the route matching actually operates on percent-escaped URLs. So the actual URL is https://coverme.co.il/product/%D7%A0%D7%A8-%D7%91%D7%99%D7%A0%D7%95%D7%A0%D7%99-tuberosejasmine/, and this does not match your route because %D7%A0%D7%A8-%D7%91%D7%99%D7%A0%D7%95%D7%A0%D7%99 is not considered to be the same as נר-בינוני.
EDIT: Unfortunately, using percent-encoding in your route pattern won't fix the problem, due to a known bug. Unfortunately, it's just not possible to match non-ASCII characters in a Workers route today. We intend to fix this, but it's hard because some sites are accidentally dependent on the broken behavior, so the fix would break them.
What you can potentially do instead is match against coverme.co.il/product/*, and then, inside your worker, check if the path also has נר-בינוני-tuberosejasmine. If it does not, then your fetch event handler should simply return without calling event.respondWith(). This will trigger "default handling" of the request, meaning it will pass through and be sent to your origin server like normal. (Note that you will still be billed for a Workers request, though.)
So, something like this:
addEventListener("fetch", event => {
if (event.request.url.includes(
"coverme.co.il/product/נר-בינוני-tuberosejasmine/")) {
event.respondWith(handle(event.request));
} else {
return; // not a match, use default pass-through handling
}
})

What if only send without recv in my Thrift client?

I'm implementing a Thrift client in order to make connection to a built-in scribe server.
Everything is going OK if I use a standard Log method, like this:
public boolean log(List<LogEntry> messages) {
boolean ret = false;
PooledClient client = borrowClient();
try {
if ((client != null) && (client.getClient() != null)) {
ResultCode result = client.getClient().Log(messages);
ret = (result != null && result.equals(ResultCode.OK));
returnClient(client);
}
} catch (Exception ex) {
logger.error(LogUtil.stackTrace(ex));
invalidClient(client);
}
return ret;
}
However, when I use send_Log instead:
public void send_Log(List<LogEntry> messages) {
PooledClient client = borrowClient();
try {
if ((client != null) && (client.getClient() != null)) {
client.getClient().send_Log(messages);
returnClient(client);
}
} catch (Exception ex) {
logger.error(LogUtil.stackTrace(ex));
invalidClient(client);
}
}
It acctually causes some problems:
Total network connection to port 1463 (default port for a scribe server) is going to increase so much, and always in a CLOSE_WAIT state.
Cause my application got stuck without throwing any error, I think it may be an issue with network connection.
what if send without recv
As this is clearly TCP, the sender will block (in blocking mode), or incur EAGAIN/EWOULDBLOCK in non-blocking mode. EDIT It is now clear that you want to send without receiving the reply. You can do that by just sending and then closing the socket, but that may cause the peer to incur ECONNRESET, which may upset it. You should really implement the application protocol correctly.
1/ Total network connection to port 1463 (default port for a scribe server) is going to increase so much, and always in a CLOSE_WAIT state.
Lots of ports in CLOSE_WAIT state indicates a socket leak on the part of the local application.
2/ Cause my application got stuck without throwing any error. I think it may be an issues with network connection.
It is an issue with sending and not receiving.
Since you labelled this as a Thrift related question, the answer is oneway.
service foo {
oneway void FireAndForget(1: some args)
}
The oneway keyword does exactly what the name suggests. You get a client implementation that only sends and does not wait for anything to be returned from the server. This rule also includes exceptions. Hence a oneway method must always be void and can't throw any exceptions.
However, when I use send_Log instead ...
client.getClient().send_Log(messages);
Neither one of the Thrift-generated send_Xxx and recv_Xxx methods are meant to be public. That's why they are usually either private or protected methods. They should not be called directly, unless you are sure that you know what you are doing (and very obviously the latter is not the case here).
And since the real question is about performance: Why don't you just delegate the call(s) into a secondary thread? That way the I/O will not block the UI.

asterisk to opensips conversion. all help appresciated

m curently working on converting an esxisting asterisk server to opensips, for better perfomance
for the most part it is working, but ive encountered an issue i cant really figure out.
asterisk is doing this :
if ("${fromourmobile}" != "") // Check if mobile Call Waiting is set to "n"
{
set(phonenumber=${FROM});
set(GROUP()=${phonenumber});
noop(Group Count: ${GROUP_COUNT(${phonenumber})});
if (${GROUP_COUNT(${phonenumber})} > 1)
{
Busy();
}
}
and this
if (${MATH(${EPOCH} % 2)} = 0)
{
set(dialhost=193.88.58.86);
Dial(SIP/${numbertodial}#${dialhost},60,wWtT);
&hangupcausecheck(${numbertodial}, ${dialhost});
switch (${DIALSTATUS})
{
case BUSY:
busy;
break;
default:
break;
}
set(dialhost=195.215.252.15);
Dial(SIP/${numbertodial}#${dialhost},60,wWtT);
&hangupcausecheck(${numbertodial}, ${dialhost});
switch (${DIALSTATUS})
{
case BUSY:
busy;
break;
default:
break;
}
i cant seem to find a similar way to do this in opensips, mostly the group_count() and the hangupcausecheck()
furthermore is there any equivalent to the $server variable from asterisk?
First think you need understand is
Asterisk is pbx-like software. Opensips is PROXY software.
There are no GROUPs, playback etc in Opensips. For programming opensips you have be expert in programming and FULLY understand how SIP protocol works.
Channel count can be emulated by using dialogs and caching servers. But it will not work if you config have any single error in BYE/CANCEL handling. There are no way check channel is active in most cases(becuase it not track channel's RTP data).
There is no application like Dial. Instead of that you have rewrite INVITE packet for proper destination/number.

How to programmatically send an email from a Flash AIR MOBILE app

I am trying to figure out how to send an email from a Flash Mobile (smartphones: blackberries, iphones, androids) app using mxml and Flash using Flash Builder 4.6. My boss told me to find out if it is possible. So far, I have been doing a lot of searching around on the internet for an answer.
I found this website: http://www.bytearray.org/?p=27, that has some classes for sending email in flash, but #1, I don't know if they work in Mobile apps, and #2, I can't find any instructions or tutorials on how to use the classes to send a simple email.
I downloaded the package from the site and imported into my project, where I am trying to send the code. But without sample code on how to simply send an email, I am not entirely sure what all do, and nor am I sure how to determine things like what port number to construct the SMTPMailer object (the SMTPMailer object is included in that package, and it takes a host string and a port number integer in it's constructor), right now I am trying 80 or 8080 for the port number, and I've tried localhost and one of our server computers, 198.162.1.109 for the host.
Anyway, I keep getting this error: Error #2044: Unhandled IOErrorEvent:. text=Error #2031: Socket Error.
Here is some of my sample code:
[Bindable]
private var mailer : SMTPMailer;
private function init() : void {
tbPass.displayAsPassword = true;
}
protected function btnClick_email(toAddress : String, fromAddress : String, pass : String) : void {
mailer = new SMTPMailer("198.168.1.109", 8080);
mailer.addEventListener(SMTPEvent.MAIL_SENT, onMailSent);
mailer.addEventListener(SMTPEvent.MAIL_ERROR, onMailError);
mailer.addEventListener(SMTPEvent.CONNECTED, onConnected);
mailer.addEventListener(SMTPEvent.DISCONNECTED, onDisconnected);
mailer.connect("hotmail.com", 8080);
mailer.authenticate(toAddress, pass);
mailer.sendHTMLMail(fromAddress, toAddress, "Subect", "Message");
}
private function onMailSent() : void {
lblEmailResult.text = "Sent Mail";
}
private function onMailError() : void {
lblEmailResult.text = "Error";
}
private function onConnected() : void {
lblEmailResult.text = "Connected";
}
private function onDisconnected() : void {
lblEmailResult.text = "Disconnected";
}
I would suggest using a back-end service to send emails, it is same as connecting to a SMTP mail server but it is more flexible.
That being said, it should work, the error you are getting is related to your host IP, are you sure you have SMTP server running on "198.168.1.109:8080"?
First check if you can send mails from it before trying to do it trough Flex, if that is OK, then you should double check socket policy files:
http://www.adobe.com/devnet/flashplayer/articles/socket_policy_files.html
Hope that helps
Can't you just use navigateToURL() for this?
Ie:
var request:URLRequest("mailto:someone#somewhere.com");
navigateToURL(request);
That's all - 2 lines :)
I've accomplished this in a commercial app I worked on. We used a native extension found in distriqt's set of tools. Google them. The full suite of tools is cheap, though if you have any issues, do not expect a quick reply. Their message tool is what you are looking for, and it is easy to use.

Signalr stops doing Callbacks after some minutes

I am using signalr 0.4 on an aspx-Page,
var hub = $.connection.FooHub;
hub.disconnected(function () {
log("Server has disconnected");
});
hub.ShowInfo = function (Info) { .... }
$("#Button1").click(function () {
hub.FooFunction('foo');
});
$.connection.hub.start();
The Hub is defined as :
public class FooHub : Hub, IDisconnect
{
~FooHub()
{
log.Debug("FooHub Destroy");
}
public FooHub()
{
log.Debug("FooHub Startup");
}
public bool FooFunction(string stuff)
{
log.Debug("Hub FooFunction");
Clients.ShowInfo(someInfo);
return true;
}
public Task Disconnect()
{
// Query the database to find the user by it's client id etc. etc.
MyController.Disconnect(Context.ConnectionId);
log.Debug("Hub Disconnnect " + Context.ConnectionId);
return null;
}
......
}
When i open the page and immediately click on Button1
it calls the Hub which in turn calls the ShowInfo-function on the page.
With Firebug i can see that signalr is using long-polling for the communcation.
So everything works as expected.
But when i then wait a couple of minutes
i see that
FooHub is destroyed
Disconnect is called in the Hub,
however on the Page there is no new connection
Firebug shows the old one still being executed
and when i then click on the Button -
FooFunction is called (i see a new connection in firebug)
FooHub is created
FooFunction is executed in the Hub (there is a line in the Log)
but ShowInfo is not executed
Is this a bug in SignalR or do i have to do something else to get the ShowInfo-call?
Update (Possible answer):
It was using a Forever-Frame and not long-polling.
In addition, the problem seems to happen mostly when using mobile internet (usb-stick) and Firefox.
Changing the transport to long-Polling seems to fix this issue.
You mention switching to long polling instead of using a forever frame but didn't say how to do that. You can specify which transports to try when starting the connection.
connection.start({ transport: ['longPolling','webSockets'] });
See https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client for more options.
Did u look at this https://github.com/SignalR/SignalR/wiki/Configuring-SignalR
Try this see. To me as per your code every thing seems to be OK. Also can you test the same in Chrome and see whats happening.

Resources