using System;
namespace CommApp
{
class Program
{
public static WebSocket web = new WebSocket();
static void Main()
{
// 启动websocket
web.WebStart();
while (true)
{
// 从窗体中读取用户输入,然后发送给web
web.Send(Console.ReadLine());
}
}
}
}
WebSocket.cs 代码如下:
using Fleck;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommApp
{
public class WebSocket
{
private List<IWebSocketConnection> allSockets = new List<IWebSocketConnection>();
public void WebStart()
{
FleckLog.Level = LogLevel.Debug;
allSockets = new List<IWebSocketConnection>();
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:2000/");
server.Start(socket =>
{
socket.OnOpen = () => { allSockets.Add(socket); };
socket.OnClose = () => { allSockets.Remove(socket); };
socket.OnMessage = message => {
try
{
Console.WriteLine("接收:" + message);
}
catch (Exception e)
{
// 立即反馈异常信息
allSockets.ToList().ForEach(s => s.Send("数据错误:" + e.Message));
}
};
});
}
/// <summary>
/// 按字节发送
/// </summary>
/// <param name="vlue"></param>
public void Send(byte[] vlue)
{
foreach (var socket in allSockets.ToList())
{
socket.Send(vlue);
}
}
/// <summary>
/// 按字符串发送
/// </summary>
/// <param name="vlue"></param>
public void Send(string vlue)
{
foreach (var socket in allSockets.ToList())
{
socket.Send(vlue);
}
}
}
}
var input = document.getElementById('sendText');
var recv = document.getElementById('recvText');
var wsImpl = window.WebSocket || window.MozWebSocket;
recv.value += "connecting to server\r\n";
// create a new websocket and connect
window.ws = new wsImpl('ws://127.0.0.1:2000/');
// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
recv.value += evt.data + '\r\n';
};
// when the connection is established, this method is called
ws.onopen = function () {
recv.value += 'connection open\r\n';
};
// when the connection is closed, this method is called
ws.onclose = function () {
recv.value += 'connection closed\r\n';
}
// 发送消息
function SendText()
{
ws.send(input.value);
}
using System;
namespace CommApp
{
class Program
{
public static WebSocket web = new WebSocket();
static void Main()
{
// 启动websocket
web.WebStart();
while (true)
{
// 从窗体中读取用户输入,然后发送给web
web.Send(Console.ReadLine());
}
}
}
}
WebSocket.cs 代码如下:
using Fleck;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CommApp
{
public class WebSocket
{
private List<IWebSocketConnection> allSockets = new List<IWebSocketConnection>();
public void WebStart()
{
FleckLog.Level = LogLevel.Debug;
allSockets = new List<IWebSocketConnection>();
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:2000/");
server.Start(socket =>
{
socket.OnOpen = () => { allSockets.Add(socket); };
socket.OnClose = () => { allSockets.Remove(socket); };
socket.OnMessage = message => {
try
{
Console.WriteLine("接收:" + message);
}
catch (Exception e)
{
// 立即反馈异常信息
allSockets.ToList().ForEach(s => s.Send("数据错误:" + e.Message));
}
};
});
}
/// <summary>
/// 按字节发送
/// </summary>
/// <param name="vlue"></param>
public void Send(byte[] vlue)
{
foreach (var socket in allSockets.ToList())
{
socket.Send(vlue);
}
}
/// <summary>
/// 按字符串发送
/// </summary>
/// <param name="vlue"></param>
public void Send(string vlue)
{
foreach (var socket in allSockets.ToList())
{
socket.Send(vlue);
}
}
}
}
var input = document.getElementById('sendText');
var recv = document.getElementById('recvText');
var wsImpl = window.WebSocket || window.MozWebSocket;
recv.value += "connecting to server\r\n";
// create a new websocket and connect
window.ws = new wsImpl('ws://127.0.0.1:2000/');
// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
recv.value += evt.data + '\r\n';
};
// when the connection is established, this method is called
ws.onopen = function () {
recv.value += 'connection open\r\n';
};
// when the connection is closed, this method is called
ws.onclose = function () {
recv.value += 'connection closed\r\n';
}
// 发送消息
function SendText()
{
ws.send(input.value);
}