我想远程管理用户打开的浏览器窗口,以便支持我的网站上的用户。我通过套接字传输点击和键盘事件来模拟一些事件,但由于浏览器
我想远程管理用户打开的浏览器窗口,以便支持我的网站上的用户。
我通过套接字传输点击和键盘事件模拟了一些事件,但由于浏览器策略,我无法访问所有事件。例如,当我想打开颜色选择器时,我收到了一条警告消息。
function triggerClickEvent(clickPosition) {
var { x, y } = clickPosition;
if (!window.clickEvent) {
window.clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
button: 1,
cancelable: true,
clientX: x,
clientY: y
});
} else {
window.clickEvent.clientX = x;
window.clickEvent.clientY = y;
}
document.elementFromPoint(x, y).dispatchEvent(clickEvent);
clickEvent.target.blur();
clickEvent.target.focus();
clickEvent.target.click();
}
function triggerKeyPressEvent(keyPress) {
var keyPressEvent = new KeyboardEvent('keydown', { 'key': keyPress.key });
document.dispatchEvent(keyPressEvent);
}
如何通过获取相关的用户权限来解决这些问题?
我叫 Luís,目前正在开发一个项目,该项目包括实施客户端/服务器系统来管理 ServiMoto 公司提供的移动服务。服务包括树木和...
我叫 Luís,目前正在开发一个项目,该项目包括实施客户端/服务器系统来管理 ServiMoto 公司提供的移动服务。服务包括树木和花园检查、消防服务干预、邮政投递和披萨外送。该系统允许客户端(自行车)连接到服务器以接收任务、更新任务状态和请求新任务。服务器管理这些请求,将任务分配给客户端,并在 CSV 文件中保存分配的任务和客户端的记录。该项目使用套接字和定义的通信协议在 C# 中实现。
目前我的两个主要代码是:
类服务器:
// Object Mutex to ensure exclusive access to CSV files
static Mutex mutex = new Mutex();
// Method to update the CSV file with the completion of a task
static void UpdateCompletedTask(string fileName, string taskId)
{
// Locks the mutex to ensure exclusive access
mutex.WaitOne();
try
{
string[] lines = File.ReadAllLines(fileName);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith(taskId))
{
// Replaces "in progress" with "completed" only if "in progress" is present
if (lines[i].Contains("in progress"))
{
lines[i] = lines[i].Replace("in progress", "completed");
}
break;
}
}
File.WriteAllLines(fileName, lines);
}
finally
{
// Releases the mutex
mutex.ReleaseMutex();
}
}
// Method to assign a new task to the client and update the corresponding CSV file
static void AllocateNewTask(string fileName, string clientId)
{
// Locks the mutex to ensure exclusive access
mutex.WaitOne();
try
{
// Logic to assign a new task to the client and update the CSV file
// For example, it may involve reading the file to find an available task and updating its status
}
finally
{
// Releases the mutex
mutex.ReleaseMutex();
}
}
// Server IP address and port
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
int port = 8888;
// Starts the server and listens for connections
server = new TcpListener(ipAddress, port);
server.Start();
Console.WriteLine("Server started...");
// Accepts client connection
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client connected!");
// Prepares network streams
NetworkStream stream = client.GetStream();
byte[] data = new byte[256];
StringBuilder response = new StringBuilder();
int bytesRead;
// Reads data received from the client
while ((bytesRead = stream.Read(data, 0, data.Length)) != 0)
{
response.Append(Encoding.ASCII.GetString(data, 0, bytesRead));
Console.WriteLine("Received message: {0}", response.ToString());
// Checks the type of received message
if (response.ToString().StartsWith("CONNECT"))
{
// Responds with success
byte[] msg = Encoding.ASCII.GetBytes("100 OK");
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Response sent: 100 OK");
}
else if (response.ToString().StartsWith("TASK_COMPLETE"))
{
// Extracts the ID of the completed task
string completedTaskId = response.ToString().Substring("TASK_COMPLETE".Length).Trim();
// Updates the corresponding CSV file
UpdateCompletedTask("Service_A.csv", completedTaskId);
// Responds with task completion confirmation
byte[] msg = Encoding.ASCII.GetBytes("TASK_COMPLETED");
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Response sent: TASK_COMPLETED");
}
else if (response.ToString() == "REQUEST_TASK")
{
// Logic to assign a new task to the client and update the CSV file
// Here you can call the AllocateNewTask() method to assign the new task
}
else if (response.ToString() == "QUIT")
{
// Responds with connection closure
byte[] msg = Encoding.ASCII.GetBytes("400 BYE");
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Response sent: 400 BYE");
// Closes the connection
client.Close();
break;
}
else
{
// Responds with error
byte[] msg = Encoding.ASCII.GetBytes("ERROR");
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Response sent: ERROR");
}
// Clears the StringBuilder for the next message
response.Clear();
}
现在客户端类:
// Server IP address and port
string serverIp = "127.0.0.1";
int port = 8888;
// Creates an instance of the TCP client
TcpClient client = new TcpClient(serverIp, port);
Console.WriteLine("Connected to server...");
// Prepares network streams
NetworkStream stream = client.GetStream();
byte[] data = new byte[256];
string response = string.Empty;
Console.WriteLine("Enter your Client ID: ");
string clientId = Console.ReadLine();
// Sends connection message
string connectMessage = "CONNECT";
byte[] connectMsg = Encoding.ASCII.GetBytes(connectMessage);
stream.Write(connectMsg, 0, connectMsg.Length);
Console.WriteLine("Message sent: {0}", connectMessage);
// Reads the server's response
int bytesReceived = stream.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data, 0, bytesReceived);
Console.WriteLine("Response received: {0}", response);
while (true)
{
Console.WriteLine("Choose an option:");
Console.WriteLine("1. Complete task");
Console.WriteLine("2. Request new task");
Console.WriteLine("3. Quit");
Console.Write("Option: ");
string option = Console.ReadLine();
switch (option)
{
case "1":
// Sends task completion message
Console.WriteLine("Enter the ID of the completed task: ");
string completedTaskId = Console.ReadLine();
string completionMessage = $"TASK_COMPLETE <{completedTaskId}>";
byte[] completionMsg = Encoding.ASCII.GetBytes(completionMessage);
stream.Write(completionMsg, 0, completionMsg.Length);
Console.WriteLine("Message sent: {0}", completionMessage);
// Reads the server's response
int completionBytesReceived = stream.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data, 0, completionBytesReceived);
Console.WriteLine("Response received: {0}", response);
break;
case "2":
// Sends request for new task
string requestMessage = "REQUEST_TASK";
byte[] requestMsg = Encoding.ASCII.GetBytes(requestMessage);
stream.Write(requestMsg, 0, requestMsg.Length);
Console.WriteLine("Message sent: {0}", requestMessage);
// Reads the server's response
int requestBytesReceived = stream.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data, 0, requestBytesReceived);
Console.WriteLine("Response received: {0}", response);
break;
case "3":
// Sends quit message
string quitMessage = "QUIT";
byte[] quitMsg = Encoding.ASCII.GetBytes(quitMessage);
stream.Write(quitMsg, 0, quitMsg.Length);
Console.WriteLine("Message sent: {0}", quitMessage);
// Reads the server's response
int quitBytesReceived = stream.Read(data, 0, data.Length);
response = Encoding.ASCII.GetString(data, 0, quitBytesReceived);
Console.WriteLine("Response received: {0}", response);
// Closes the connection and exits the loop
client.Close();
return;
default:
Console.WriteLine("Invalid option.");
break;
}
}
问题是,当我想使用 UpdateCompletedTask 函数声明任务完成时,csv 文件不会改变,始终保持不变。有人能帮帮我吗?
我多次更改了函数的代码,因为我认为问题出在 UpdateCompletedTask 函数中,但仍然没有任何变化。