I am currently trying to feed my socket.io server with data from my C# client. But I am not sure how to receive the message on the server.
My server code:
const io = require('socket.io')(9000);
io.on('connection', (socket) => {
console.log('Connected');
}
First of all I don't know which event I have to listen to, but nevertheless I am unable to send data to my server using the following client (which uses Websocket-sharp) code:
private void init()
{
// start socket connection
using (var ws = new WebSocket("ws://localhost:9000/socket.io/?EIO=2&transport=websocket"))
{
ws.OnMessage += (sender, e) =>
API.consoleOutput("Message: " + e.Data);
ws.OnError += (sender, e) =>
API.consoleOutput("Error: " + e.Message);
ws.Connect();
ws.Send("server");
}
}
The connection works, but how do I receive the message of the server? The sending does not fire an error, therefore I think it does work.
I've gotten this working for a UWP app that connects to a node.js server. Basically what I do is connect to a URL that looks like ws://localhost:4200/socket.io/?EIO=3&transport=websocket
the port number being something we chose.
once that is set I connect to the node.js socket io library via the following lines of code.
private async Task ConnectWebsocket() {
websocket = new MessageWebSocket();
Uri server = new Uri(WebSocketURI); //like ws://localhost:4300/socket.io/?EIO=3&transport=websocket
websocket.Control.MessageType = SocketMessageType.Utf8;
websocket.MessageReceived += Websocket_MessageReceived;
websocket.Closed += Websocket_Closed;
try {
await websocket.ConnectAsync(server);
isConnected = true;
writer = new DataWriter(websocket.OutputStream);
}
catch ( Exception ex ) // For debugging
{
// Error happened during connect operation.
websocket.Dispose();
websocket = null;
Debug.Log("[SocketIOComponent] " + ex.Message);
if ( ex is COMException ) {
Debug.Log("Send Event to User To tell them we are unable to connect to Pi");
}
return;
}
}
`
at this point your socket io on "connection" should fire on your server
then you can emit events to it like normal. except the C# socket code does not discriminate various channels so you must do so on your own. below is how we do it (aka SocketData and SocketIOEvent are classes we have defined)
private void Websocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) {
try {
using ( DataReader reader = args.GetDataReader() ) {
reader.UnicodeEncoding = UnicodeEncoding.Utf8;
try {
string read = reader.ReadString(reader.UnconsumedBufferLength);
//read = Regex.Unescape(read);
SocketData socc = SocketData.ParseFromString(read);
if (socc != null ) {
Debug.Log(socc.ToString());
SocketIOEvent e = new SocketIOEvent(socc.channel, new JSONObject( socc.jsonPayload));
lock ( eventQueueLock ) { eventQueue.Enqueue(e); }
}
}
catch ( Exception ex ) {
Debug.Log(ex.Message);
}
}
} catch (Exception ex ) {
Debug.Log(ex.Message);
}
}
in our specific application we did not need to send messages to our server, so for that I do not have a good answer.
Related
Signalr client code is causing performance issue is Windows Service. Windows service is acting as signalr client.
What we are trying to do:
Windows service is having one timer, which executes the method(ConnectToSignalRServer). If somehow the connection gets closed, there is one event (Connection.Closed += Connection_Closed), which will again try to establish the connection by calling the method(ConnectToSignalRServer). A while loop is being used in the event (Connection.Closed += Connection_Closed) to try reconnecting.
Please find the sample code below and let me know if any issues with the code.
private static HubConnection Connection = null;
//When the service starts, this method would be called.
public static bool ConnectToSignalRServer()
{
try
{
string Url = "http://www.samplesignalrserver.com";
Connection = new HubConnection(Url);
var myHub = Connection.CreateHubProxy("SignalHub");
Connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
}
else
{
Connection.Closed += Connection_Closed;
}
}).Wait();
//Method(RequestData) would be called upon receiveng message from server
myHub.On<string>("GetMessgeFromServer", type =>
{
Task.Run(() => RequestData(type));
});
//Method(GetHostName) would be called in server
myHub.Invoke<string>("GetHostName", BLConstants.strHostName);
return true;
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
return false;
}
//Establish the connection, if the the connection would be closed
private static void Connection_Closed()
{
try
{
while (true)
{
Thread.Sleep(10000);
bool connected = ConnectToSignalRServer();
if (connected)
{
break;
}
}
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
}
thanks in advance for anything.
Im making a server-client program, in which the server(application) creates a server and the client connects to it, i will also have a textbox and a button on the server application and whenever i write something on that textbox and press the button, it will send to the client application(there is only a textbox in this application, the only thing this application does is receive strings from the server application).
I think it works, kinda, but not the way i want it.
I can make the connection and also send and receive the information from the textboxs, but only if i run the server application first(to create the server). The problem is that if i dont run the server application first(to create the server), the client application won't connect, or even try to.
Example of "Error"(i guess you can call it an error):
If i run the client application first and then the server application, the client application just won't connect to the server that the server application created, i made a loop that basically verifies if the client is cnnected, if it is then it starts receiving the information, if not(else) waits for 3 seconds and tries to reconnect again. but when it tries to reconnect it doesnt work.
Any ideas?
CODE IN C#:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) //connect to server
{
client = new TcpClient();
IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse("192.168.254.34"), 123); // sincronizacao do IP com a porta
try
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = client.BeginConnect(IPAddress.Parse("192.168.254.34"), 123, null, null);
bool success = result.AsyncWaitHandle.WaitOne(3000, true);
while (success)
{
if (client.Connected)
{
STW = new StreamWriter(client.GetStream());
STR = new StreamReader(client.GetStream());
STW.AutoFlush = true;
backgroundWorker1.RunWorkerAsync(); // Começar a receber dados em background
backgroundWorker1.WorkerSupportsCancellation = true; // possibilidade de cancelar o fio
}
else
{
int milliseconds = 3000;
Thread.Sleep(milliseconds);
MessageBox.Show("swag do elias!");
client.Connect(IP_End);
}
}
}
catch (SocketException exception)
{
MessageBox.Show("O erro é:", exception.Source);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // Receber dados
{
while(client.Connected) //enquanto o cliente tiver conectado vai ler o que servidor diz em chat
{
try
{
receive = STR.ReadLine();
this.textBox2.Invoke(new MethodInvoker(delegate () { textBox2.Text=(receive + "\n\r"); }));
receive = "";
}
catch(Exception x)
{
MessageBox.Show(x.Message.ToString());
}
}
}
}
}
If the server isn't running yet, then success is always going to be false as a result of this line:
bool success = result.AsyncWaitHandle.WaitOne(3000, true);
Since success will always be false, the code inside of your while(success) block will never be executed.
Rearranging your code to something like this might be helpful:
client = new TcpClient();
bool success = false;
while (success == false)
{
try
{
IAsyncResult result = client.BeginConnect(IPAddress.Parse("192.168.254.34"), 123, null, null);
success = result.AsyncWaitHandle.WaitOne(3000, true);
}
catch (Exception ex)
{
success = false;
MessageBox.Show("error connecting: " + ex.Message + " : " + ex.StackTrace);
}
}
// NOW, by the time you reach this point, you KNOW that success == true and that you're connected, and you can proceed with the rest of your code
STW = new StreamWriter(client.GetStream());
STR = new StreamReader(client.GetStream());
STW.AutoFlush = true;
backgroundWorker1.RunWorkerAsync(); // Começar a receber dados em background
backgroundWorker1.WorkerSupportsCancellation = true; // possibilidade de cancelar o fio
I'm creating a game in which I use TCP/IP connection. The problem is that I'm using .Invoke to help me receive and send message.
The program goes like this: I'm my first window, i'm starting and connecting to the server like this :
{
TcpListener listener = new TcpListener(IPAddress.Any, this.port);
listener.Start();
try {
this.client = listener.AcceptTcpClient();
gameWindow = new GameWindow(this.client, true);
gameWindow.StartGame();
}
}
then i'm connecting to it like this:
{
IPEndPoint ipEnd = new IPEndPoint(this.serverIP, this.port);
{
try {
client.Connect(ipEnd);
if (client.Connected) {
gameWindow = new GameWindow(this.client, false);
gameWindow.StartGame();
}
}
}
The constructor for gameWindow (which is a form) looks like this:
public GameWindow(TcpClient thisClient, bool isServer)
{
InitializeComponent();
this.client = thisClient;
this.reader = new StreamReader(thisClient.GetStream());
this.writer = new StreamWriter(thisClient.GetStream());
this.writer.AutoFlush = true;
}
I must wait for the server to send a message to the client, and then start the client ( I have a function .startGame() that uses .ShowDialog() and creates some pictureBoxs)
But nowhere I can get my handle created. I've tried to put this.createHandle() (read about it here) into GameWindow_Load but still not works. If I try to send a message with:
workerSendData.RunWorkerAsync(); I get:
Additional information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
What can I do to get my handler created? Using Thread.Sleep will sleep my whole UI, which does not work (a "solution" found on the internet)
My code for sending message :
private void workerSendData_DoWork(object sender, DoWorkEventArgs e)
{
if (client.Connected) {
this.writer.WriteLine(this.toSend); // aici trimitem datele.
// de modificat : aici vom adauga in lista noastra miscarile.
this.Invoke(new MethodInvoker(delegate () { MessageBox.Show("Me:" + this.toSend + "\n"); }));
}
else {
MessageBox.Show("Send failed");
}
workerSendData.CancelAsync();
}
My code for receiving data:
private void workerReceiveData_DoWork(object sender, DoWorkEventArgs e)
{
while (client.Connected) {
try {
this.received = this.reader.ReadLine();
this.myTurn = true;
this.Invoke(new MethodInvoker(delegate () {
MessageBox.Show("This has been received: " + this.received);
/*this.tbReceive.AppendText("You:" + this.received + "\n");*/
}));
this.received = "";
}
catch (Exception x) {
MessageBox.Show(x.Message.ToString());
}
}
}
It seems that you cannot invoke an action before the Window is fully initialized and loaded. Assuming you are working in Windows Forms, there is a solution provided by #Greg D on this question, but it doesn't be to be the safest way to go.
I would suggest that you try to find a way to start the worker only after the window is loaded (for example using the Loaded event), so that the handle is definitely ready and this situation does not occur.
Can anybody help me in connecting FCM (Firebase Cloud Messaging) server using XMPP library using C#?
I want to send Push notification using XMPP.
I am trying to connect it using Sharp.Xmpp as below,
public class XmppConnector
{
public XmppConnector()
{
string hostname = "fcm-xmpp.googleapis.com";
string username = "exmple#gcm.googleapis.com";
string password = "example";
using (XmppClient client = new XmppClient(hostname, username, password))
{
try
{
// Setup any event handlers before connecting.
client.Message += OnNewMessage;
// Connect and authenticate with the server.
client.Connect();
}
catch (Exception e)
{
throw e;
}
}
}
/// <summary>
/// Invoked whenever a new chat-message has been received.
/// </summary>
private void OnNewMessage(object sender, Sharp.Xmpp.Im.MessageEventArgs e)
{
Console.WriteLine("Message from <" + e.Jid + ">: " + e.Message.Body);
//throw new NotImplementedException();
}
}
But it does not work, I even tried with another port (5235 and 5236). If you have any examples, do share.
Thanks in advance.
I'm using https://github.com/ForNeVeR/Jabber-Net
myJabberClient = new JabberClient();
myJabberClient.User = Globales.FIRABASE_MESSAGING_USER + "#gcm.googleapis.com";
myJabberClient.Password = Globales.FIREBASE_MESSAGING_KEY;
myJabberClient.Server = Globales.FIRABASE_GCM_XMPP_SERVER;
myJabberClient.Resource = "MyTestClient";
myJabberClient.Port = 5235;
myJabberClient.AutoReconnect = 1;
myJabberClient.AutoPresence = false;
myJabberClient.AutoRoster = false;
myJabberClient.KeepAlive = 10;
myJabberClient.PlaintextAuth = true;
myJabberClient.AutoLogin = true;
myJabberClient.AutoStartTLS = false;
myJabberClient.SSL = true;
myJabberClient.OnMessage += async (s, msg) => {
// Do stuff here
}
myJabberClient.Connect();
I do push notifications with HTTP API.
I'm only having problem sending ACK message of upstream received messages to CCS Server, because the ":" character in some registrarion ids
I did it using Jabber-net library, which you can download from,
here, it includes library and some examples to understand about how to connect to fcm,also it can download through the nuget package manager package name is
jabber-net select the latest version. check here for configured sample.
I am trying to develop an application in android to sent the gps data to my pc .the android part is :
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
float latitude = (float) (location.getLatitude());
float longitude = (float) (location.getLongitude());
showMessage("Student Details", "Latitude: " + latitude + ", Longitude: " + longitude);
Log.i("Geo_Location", "Latitude: " + latitude + ", Longitude: " + longitude);
try {
Socket socket = new Socket("192.168.1.5",5000);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF("HELLO_WORLD");
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the c# code or server code is :
public AsyncCallback pfnWorkerCallBack;
private Socket m_mainSocket;
private Socket[] m_workerSocket = new Socket[25];
private int m_clientCount = 0;
private string ipaddress;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
startfun();
}
public void startfun()
{
try
{
// DrawMapPersian();
ipaddress = "192.168.1.5";
// Check the port value
string portStr = "5000";
int port = System.Convert.ToInt32(portStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
listBox1.Items.Add("Server Started...");
// Start listening...
m_mainSocket.Listen(20);
listBox1.Items.Add("Server Listening for ...");
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (Exception qqq)
{
using (StreamWriter writer =
new StreamWriter(#"e:\a.txt"))
{
writer.Write(qqq.Message);
}
}
}
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket socketData = (SocketPacket)asyn.AsyncState;
int iRx = 0;
// Complete the BeginReceive() asynchronous call by EndReceive() method
// which will return the number of characters written to the stream
// by the client
iRx = socketData.m_currentSocket.EndReceive(asyn);
string res = GetParameters(socketData.dataBuffer);
Console.WriteLine(res.ToString());
}
another thing that i should say i add this permissions to manifest <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" />
I connected the mobile phone to the wireless network ,i mean the mobile can access my ip .
The problem is the gps data is generated so slowly i don't know why ?another problem is the android application doesn't send any data to server ,
Client
To accelerate the location update, you have to specify to the Activity the location update interval when setting the LocationListener implementation.
Something like :
LocationListener impl = new MyLocationListenerImpl();
long interval = 4000;//change your code here
setLocationListener(impl, interval);
(Note that the code above is more like a pseudo code, since It's been a long time since I have touched LocationListener).
Server
The code you provided does not mention the "OnDataReceived" member function. So I think that you should dig in this way.
Unless sockets are used on purpose, consider using Http Protocol and frameworks like ASP.NET and loopj Async Http to pass data to a server, it's easier !