我用C#制作了一个桌面程序 Web套接字((use:https://www.nuget.org/packages/WebSocketSharp/1.0.3-rc11)) 并且服务器端脚本是PHP Web套接字((使用:http://socketo.me/)) 但是我遇到了一些错误。
1-当我调用Send方法时,readyState切换为“ close”,我必须在Send之前调用Connect方法。
2-当程序空闲一会儿时,连接会关闭
请帮助我2个错误。 非常感谢
我的代码c#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using WebSocketSharp;
using System.Globalization;
namespace MyApp
{
public partial class Form1 : Form
{
public WebSocket ws = new WebSocket("ws://example.com:4563");
private static readonly HttpClient client = new HttpClient();
public int request_id;
public bool online;
public async Task<String> login(String id, String password)
{
var values = new Dictionary<string, string>
{
{ "id",id },
{ "password",password }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://example.com/Login", content);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
public void websocket()
{
button1.Enabled = button4.Enabled = button5.Enabled = false;
ws.OnOpen += (sende, E) =>
{
//conncect app
};
ws.OnClose += (sende, E) =>
{
};
ws.OnMessage += (sende, E) =>
{
dynamic response = JsonConvert.DeserializeObject(E.Data);
switch ((String)response.type)
{
case "push_for_app":
{
label10.Text = (String)response.response;
turn_count = (String)response.response;
if ((String)response.response == "0")
{
button1.Enabled = button4.Enabled = button5.Enabled = false;
}
else if (online) button1.Enabled = true;
}
break;
case "request":
{
switch ((String)response.method)
{
case "logout":
{
if ((String)response.response == "true")
{
groupBox1.Visible = false;
groupBox4.Visible = false;
groupBox1.Visible = false;
groupBox4.Visible = false;
groupBox2.Visible = true;
}
}
break;
case "get_turn_count":
{
label10.Text = (String)response.response;
turn_count = (String)response.response;
if ((String)response.response == "0")
{
button1.Enabled = button4.Enabled = button5.Enabled = false;
}
else if (online) button1.Enabled = true;
}
break;
case "set_online":
{
if ((String)response.response == "is_online")
{
button2.Visible = true;
online = true;
{
var template = new
{
type = "request",
method = "get_turn_count"
};
String output = JsonConvert.SerializeObject(template);
ws.Connect();
ws.Send(output);
}
}
}
break;
case "set_offline":
{
if ((String)response.response == "is_offline")
{
button8.Visible = true;
button1.Enabled = false;
online = false;
}
}
break;
default: break;
}
}
break;
default: break;
}
};
var List = new List<KeyValuePair<String, String>> {
new KeyValuePair<String, String>("api-key", "jshTRHNSDB54n7y4e5nhty"),
new KeyValuePair<String, String>("secret-key", secret_key),
new KeyValuePair<String, String>("type", "app")
};
ws.CustomHeaders = List;
ws.Connect();
}
//login
private void button7_Click(object sender, EventArgs e)
{
Task<String> task = Task.Run(async () => await login(textBox1.Text, textBox3.Text));
task.Wait();
dynamic res = JsonConvert.DeserializeObject(task.Result);
if (res.state == "true")
{
secret_key = res.message;
set_secret(secret_key);
groupBox1.Visible = true;
groupBox4.Visible = true;
groupBox2.Visible = false;
button8.Visible = true;
//connect socket
websocket();
}
else { label11.Text = res.message; }
}
//start app
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(SystemInformation.VirtualScreen.Width - this.Width, SystemInformation.VirtualScreen.Height - this.Height);
this.BringToFront();
this.TopMost = true;
this.Focus();
}
//set offline
private void button2_Click(object sender, EventArgs e)
{
var template = new
{
type = "request_from_pharmacy",
method = "set_offline"
};
String output = JsonConvert.SerializeObject(template);
ws.Connect();
ws.Send(output);
button2.Visible = false;
button8.Visible = true;
}
//set online
private void button8_Click(object sender, EventArgs e)
{
var template = new
{
type = "request",
method = "set_online"
};
String output = JsonConvert.SerializeObject(template);
ws.Connect();
ws.Send(output);
button8.Visible = false;
button2.Visible = true;
}
}
}