I'm having a problem using WebSocket4Net library.
I have the event websocket_opened and when this event is raised on open if I don't send any messages I have an Exception
System.Exception : You must send data by websocket after websocket is
opened
After I send this message I can't send any other messages. I execute the send command but I have no exceptions and it isn't working and If I check the state the websocket is open
If I close the Socket on the opened event and open it again in the closed_event I can send another message without problems.
So, if I want to send multiple messages I have to disconnect after sending a message and reconnect again to send the next message.
private static void websocket_Opened(object sender, EventArgs e)
{
websocket.Send(msg);
websocket.Close();
}
private static void websocket_Closed(object sender, EventArgs e)
{
websocket.Open();
}
Is this normal? Can you help me with this please?
I had a similar questions when I first time used WebSocket4Net.
A good thing about this library that it has a repository on github with many examples of use (tests). These tests was very helpful for me to understand how it should work.
UPDATE:
I saw Fred's comment below and I think I really need to add an example.
So here is it, I've used this api:
using SuperSocket.ClientEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebSocket4Net;
namespace TestProj
{
public class WebSocketTest
{
static string webSocketUri = "wss://ws.binaryws.com/websockets/v3?app_id=1089";
static void Main(string[] args)
{
var socketManager = new WebSocketManager(webSocketUri);
socketManager.Send("{\"time\": 1}");
socketManager.Send("{\"ping\": 1}");
socketManager.Close();
Console.WriteLine("Console can be closed...");
Console.ReadLine();
}
}
public class WebSocketManager
{
private AutoResetEvent messageReceiveEvent = new AutoResetEvent(false);
private string lastMessageReceived;
private WebSocket webSocket;
public WebSocketManager(string webSocketUri)
{
Console.WriteLine("Initializing websocket. Uri: " + webSocketUri);
webSocket = new WebSocket(webSocketUri);
webSocket.Opened += new EventHandler(websocket_Opened);
webSocket.Closed += new EventHandler(websocket_Closed);
webSocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
webSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
webSocket.Open();
while (webSocket.State == WebSocketState.Connecting) { }; // by default webSocket4Net has AutoSendPing=true,
// so we need to wait until connection established
if (webSocket.State != WebSocketState.Open)
{
throw new Exception("Connection is not opened.");
}
}
public string Send(string data)
{
Console.WriteLine("Client wants to send data:");
Console.WriteLine(data);
webSocket.Send(data);
if (!messageReceiveEvent.WaitOne(5000)) // waiting for the response with 5 secs timeout
Console.WriteLine("Cannot receive the response. Timeout.");
return lastMessageReceived;
}
public void Close()
{
Console.WriteLine("Closing websocket...");
webSocket.Close();
}
private void websocket_Opened(object sender, EventArgs e)
{
Console.WriteLine("Websocket is opened.");
}
private void websocket_Error(object sender, ErrorEventArgs e)
{
Console.WriteLine(e.Exception.Message);
}
private void websocket_Closed(object sender, EventArgs e)
{
Console.WriteLine("Websocket is closed.");
}
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
Console.WriteLine("Message received: " + e.Message);
lastMessageReceived = e.Message;
messageReceiveEvent.Set();
}
}
}
i think that this will resolve your problem
websocket.Open();
private static void websocket_Opened(object sender, EventArgs e)
{
websocket.Send(msg);
websocket.Close();
}
private static void websocket_Closed(object sender, EventArgs e)
{
//DO SOMETHINGS
}
Related
I am trying to create a TCP/IP client/server app that runs on my computer.The server seems to works perfectly but the client can't send any data.
Here is my code for the client:
using SimpleTCP;
using System.Text;
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// implementing simple tcp client
SimpleTcpClient client;
//SimpleTcpServer server;
private void btnConnect_Click(object sender, EventArgs e)
{
btnConnect.Enabled = false;
// server.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
//btnSend.Enabled = false;
client = new SimpleTcpClient();
client.StringEncoder = Encoding.UTF8;
client.DataReceived += Client_DataReceived;
}
private void Client_DataReceived(object? sender, SimpleTCP.Message e)
{
textStatus.Invoke((MethodInvoker)delegate ()
{
textStatus.Text += e.MessageString;
});
}
private void btnSend_Click(object sender, EventArgs e)
{
client.Write(textMessage.Text);
}
}
}
Here is code for the server:
using SimpleTCP;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace TCPIPDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (server.IsStarted)
{
server.Stop();
}
}
// implementing simpletcpserver class
SimpleTcpServer server;
private void Form1_Load(object sender, EventArgs e)
{
server = new SimpleTcpServer();
server.Delimiter = 0x13;
server.StringEncoder = Encoding.UTF8;
server.DataReceived += Server_DataReceived;
}
private void Server_DataReceived(object? sender, SimpleTCP.Message e)
{
textStatus.Invoke((MethodInvoker)delegate ()
{
textStatus.Text += e.MessageString;
e.ReplyLine(string.Format("You said:{0}",e.MessageString));
});
}
private void btnStart_Click(object sender, EventArgs e)
{
textStatus.Text += "Server starting ...";
//System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(textHost.Text));
IPAddress ip = IPAddress.Parse(textHost.Text);
server.Start(ip, Convert.ToInt32(textPort.Text));
}
}
}
The error I get is the following:
System.Exception: 'Cannot send data to a null TcpClient (check to see if Connect was called)'
you are missing a call to connect the client to the server, there is a Connect method with arguments to provide address and port
https://github.com/BrandonPotter/SimpleTCP/blob/master/SimpleTCP/SimpleTcpClient.cs#L34
The problem is described quite clearly in the exception. You need to call .Connect(URL, PORT) on a new instance of SimpleTcpClient before using it.
For example:
var client = new SimpleTcpClient().Connect("127.0.0.1", 8910);
The official GitHub has more information, please check their readme.
I try to connect but it never ends up connecting. It doesn't show me any error, am I putting in the wrong place the data? Do I have to configure something in Unity?
I'm using SocketIoClient c# for the client. The server uses laravel and PHP
I suspect that I'm setting wring the SocketIOOptions. Already tried other 2 plugins, and none of them worked.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using SocketIOClient;
class Program : MonoBehaviour
{
public static Program instance = null;
async void Start()
{
Debug.Log("start code");
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
await Main();
}
public async Task Main()
{
Debug.Log("start main");
var uri = new Uri("ws://ws.testing.bet:6781");
var socket = new SocketIO(uri, new SocketIOOptions
{
ConnectionTimeout = TimeSpan.FromSeconds(120),
Query = new Dictionary<string, string>
{
{"key", "12356"},
{"cluster", "mt1"},
{"forceTLS", "true"},
{"enabledTransports", "ws,wss"}
},
});;
socket.OnConnected += Socket_OnConnected;
socket.OnPing += Socket_OnPing;
socket.OnPong += Socket_OnPong;
socket.OnDisconnected += Socket_OnDisconnected;
socket.OnReconnectAttempt += Socket_OnReconnecting;
socket.OnError += Socket_OnError;
socket.OnAny((name, response) =>
{
Debug.Log(name);
Debug.Log(response);
});
socket.On("RealTimeEvent", response =>
{
Debug.Log("Message: " + response.GetValue<string>());
});
await socket.ConnectAsync();
Debug.Log("TRYING");
}
private static void Socket_OnReconnecting(object sender, int e)
{
Debug.Log($"{DateTime.Now} Reconnecting: attempt = {e}");
}
private static void Socket_OnError(object sender, string e)
{
Debug.Log("Error: " + e);
}
private static void Socket_OnDisconnected(object sender, string e)
{
Debug.Log("disconnect: " + e);
}
private static async void Socket_OnConnected(object sender, EventArgs e)
{
Debug.Log("Socket_OnConnected");
var socket = sender as SocketIO;
Debug.Log("Socket.Id:" + socket.Id);
await socket.EmitAsync("hi", DateTime.Now.ToString());
}
private static void Socket_OnPing(object sender, EventArgs e)
{
Debug.Log("Ping");
}
private static void Socket_OnPong(object sender, TimeSpan e)
{
Debug.Log("Pong: " + e.TotalMilliseconds);
}
}
Im trying to listen to predictions and channel point rewards. But PubSub is completely silent and I can't figure out why.
using TwitchLib.PubSub;
using TwitchLib.PubSub.Events;
namespace PubSub_app
{
class PubSub
{
TwitchPubSub client;
public PubSub()
{
client = new TwitchPubSub();
client.OnPubSubServiceConnected += Pubsub_OnPubSubServiceConnected;
client.OnChannelPointsRewardRedeemed += Pubsub_OnChannelPointsRewardRedeemed;
client.OnPrediction += Pubsub_OnPrediction;
client.Connect();
}
private void Pubsub_OnPubSubServiceConnected(object sender, System.EventArgs e)
{
client.ListenToChannelPoints("62651386");
client.ListenToPredictions("62651386");
Console.WriteLine("PubSub Connected");
}
private void Pubsub_OnPrediction(object sender, OnPredictionArgs e)
{
Console.WriteLine("Prediction");
Console.WriteLine(e.Title);
}
private void Pubsub_OnChannelPointsRewardRedeemed(object sender, OnChannelPointsRewardRedeemedArgs e)
{
Console.WriteLine("Points redeemed");
Console.WriteLine(e.RewardRedeemed);
}
}
}
The only thing that console displays that its connected
You are missing the client.SendTopics(accessToken); line in Pubsub_OnPubSubServiceConnected.
Currently I am using WebSocket-Sharp. I am able to connect to the server through my application and I am able to send a Client.Send(Move.HeadNod); to the server on button click. However even though I declared
private WebSocket client;
const string host="ws://localhost:80";
public Form1()
{
InitializeComponent();
client=new WebSocket(host);
client.connect();
Client.OnMessage+=client_OnMessage
}
where:
client_OnMessage(object sender,MessageEventArgs e)
{
textbox1.text=convert.tostring(e);
client.send(move.headleft);
}
I am still unable to get a response from the server and continue sending command afterwards.
Edit
void Client_OnMessage(object sender,MessageEventArgs e)
{
if(e.IsText)
{
edata=e.data;
return;
}
else if(e.IsBinary)
{
Textbox1.Text=Convert.Tostring(e.RawData);
return;
}
}
This is the complete code that works on my machine. Put a break-point in both event handlers to see what happens. Maybe your web socket server throws an exception and you just don't know it:
public partial class Form1 : Form
{
private readonly WebSocket _client;
public Form1()
{
InitializeComponent();
_client = new WebSocket("ws://echo.websocket.org");
_client.OnMessage += Ws_OnMessage;
_client.OnError += Ws_OnError;
_client.Connect();
}
private void Ws_OnError(object sender, ErrorEventArgs e)
{
}
private void Ws_OnMessage(object sender, MessageEventArgs e)
{
if (e.IsText)
{
Invoke(new MethodInvoker(delegate () {
textBox1.Text = e.Data;
}));
}
else if (e.IsBinary)
{
Invoke(new MethodInvoker(delegate () {
textBox1.Text = Convert.ToString(e.RawData);
}));
}
}
private void button1_Click(object sender, System.EventArgs e)
{
_client.Send("Hi");
}
}
I'm using WMI to monitor start and stop processes on Win XP machine
My code goes like this:
ManagementEventWatcher m_Create;
ManagementEventWatcher m_Delete;
private void SetMonitors()
{
string queryStartTrace = "SELECT * FROM Win32_ProcessStartTrace";
string queryStopTrace = "SELECT * FROM Win32_ProcessStopTrace";
m_Create = new ManagementEventWatcher(queryStartTrace);
m_Delete = new ManagementEventWatcher(queryStopTrace);
m_Create.EventArrived += new EventArrivedEventHandler(this.OnCreationArrived_Event);
m_Delete.EventArrived += new EventArrivedEventHandler(this.OnDeletionArrived_Event);
}
private void OnCreationArrived_Event(object sender, EventArrivedEventArgs e){...}
private void OnDeletionArrived_Event(object sender, EventArrivedEventArgs e){...}
Everything works fine. But suddenly it stops working, don't know why. Only after restart my machine it returns to work.
Edit 1
As #Alexandru helped me, I assigned the watchers to stopped and disposed events:
m_Create.Stopped += new StoppedEventHandler(watcherCreate_Stopped);
m_Create.Disposed += new EventHandler(watcherCreate_Disposed);
m_Delete.Stopped += new StoppedEventHandler(watcherDelete_Stopped);
m_Delete.Disposed += new EventHandler(watcherDelete_Disposed);
And added those methods:
void watcherCreate_Stopped(object sender, StoppedEventArgs e)
{
if (m_activeWatchers)
m_watcherCreate.Start();
}
void watcherCreate_Disposed(object sender, EventArgs e)
{
if (m_activeWatchers)
m_watcherCreate.Start();
}
void watcherDelete_Disposed(object sender, EventArgs e)
{
if (m_activeWatchers)
m_watcherDelete.Start();
}
void watcherDelete_Stopped(object sender, StoppedEventArgs e)
{
if (m_activeWatchers)
m_watcherDelete.Start();
}
Now I'm dealing with an interesting problem, the stopped event fired -> and then there is there are the calls m_Create.Start(), m_Delete.Start() and then stopped event fired -> and so on until full quota...
Edit 2
Found this link ManagementEventWatcher stops raising EventArrived. with no helpful answer but with some hint-
Should I unregister WMI events when my program closes?
Any help?
Try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace EventWatcher
{
class Program
{
static void Main(string[] args)
{
StartMonitoringProcessCreation();
StartMonitoringProcessTermination();
Console.ReadLine();
}
private static void StartMonitoringProcessCreation()
{
ManagementEventWatcher startWatcher = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStartTrace");
startWatcher.EventArrived += new EventArrivedEventHandler(startWatcher_EventArrived);
startWatcher.Stopped += new StoppedEventHandler(startWatcher_Stopped);
startWatcher.Disposed += new EventHandler(startWatcher_Disposed);
startWatcher.Start();
}
private static void StartMonitoringProcessTermination()
{
ManagementEventWatcher stopWatcher = new ManagementEventWatcher("SELECT * FROM Win32_ProcessStopTrace");
stopWatcher.EventArrived += new EventArrivedEventHandler(stopWatcher_EventArrived);
stopWatcher.Stopped += new StoppedEventHandler(stopWatcher_Stopped);
stopWatcher.Disposed += new EventHandler(stopWatcher_Disposed);
stopWatcher.Start();
}
static void startWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Got creation event.");
}
static void startWatcher_Stopped(object sender, StoppedEventArgs e)
{
Console.WriteLine("The startWatcher has stopped. Disposing it.");
((ManagementEventWatcher)sender).Dispose();
}
static void startWatcher_Disposed(object sender, EventArgs e)
{
Console.WriteLine("The startWatcher has been disposed. Restarting it.");
StartMonitoringProcessCreation();
}
static void stopWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Got termination event.");
}
static void stopWatcher_Stopped(object sender, StoppedEventArgs e)
{
Console.WriteLine("The stopWatcher has stopped. Disposing it.");
((ManagementEventWatcher)sender).Dispose();
}
static void stopWatcher_Disposed(object sender, EventArgs e)
{
Console.WriteLine("The stopWatcher has been disposed. Restarting it.");
StartMonitoringProcessTermination();
}
}
}
Edit: Because of what you've told me, it may just be easier for you to do something like this...
new Thread(() =>
{
while (true)
{
Process[] list = Process.GetProcessesByName("yourProcessName");
if (list.Length == 0)
Console.WriteLine("Not running.");
else
Console.WriteLine("Running.");
Thread.Sleep(1000);
}
}).Start()