classProgram
{
staticvoidMain(string[] args)
{
FleckLog.Level = LogLevel.Debug;
varallSockets =newList<IWebSocketConnection>();
varserver =newWebSocketServer("ws://0.0.0.0:7181");
server.Start(socket =>
{
socket.OnOpen = () =>
{
Console.WriteLine("Open!");
allSockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine("Close!");
allSockets.Remove(socket);
};
socket.OnMessage = message =>
{
Console.WriteLine(message);
allSockets.ToList().ForEach(s => s.Send("Echo: "+ message));
};
socket.OnBinary = file => {
stringpath = ("D:/test.txt");
FileStream fs =newFileStream(path, FileMode.Create);
fs.Write(file, 0, file.Length);
fs.Close();
};
});
varinput = Console.ReadLine();
while(input !="exit")
{
foreach(varsocketinallSockets.ToList())
{
socket.Send(input);
}
input = Console.ReadLine();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>websocket client</title>
<scripttype="text/javascript">
var start = function () {
var inc = document.getElementById('incomming');
var wsImpl = window.WebSocket || window.MozWebSocket;
var form = document.getElementById('sendForm');
var input = document.getElementById('sendText');
inc.innerHTML += "connecting to server ..<br/>";
// create a new websocket and connect
window.ws = new wsImpl('ws://localhost:7181/');
// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
inc.innerHTML += evt.data + '<br/>';
};
// when the connection is established, this method is called
ws.onopen = function () {
inc.innerHTML += '.. connection open<br/>';
};
// when the connection is closed, this method is called
ws.onclose = function () {
inc.innerHTML += '.. connection closed<br/>';
}
form.addEventListener('submit', function (e) {
e.preventDefault();
var val = input.value;
ws.send(val);
input.value = "";
});
}
window.onload = start;
</script>
</head>
<body>
<formid="sendForm">
<inputid="sendText" placeholder="Text to send" />
</form>
<preid="incomming"></pre>
</body>
</html>
//實現(xiàn)聊天窗口與文件發(fā)送
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPEhtml>
<html>
<head>
<title>WebSocket Chat Client</title>
<metacharset="utf-8" />
<scripttype="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.js"></script>
<scripttype="text/javascript" >
//判讀瀏覽器是否支持websocket
$().ready(function() {
if ( !window.WebSocket ) {
alert("童鞋, 你的瀏覽器不支持該功能啊");
}
});
//在消息框中打印內(nèi)容
function log(text) {
$("#log").append(text+"
");
}
//全局的websocket變量
var ws;
//創(chuàng)建連接
$(function() {
$("#uriForm").submit(function() {
log("準(zhǔn)備連接到" + $("#uri").val());
ws = new WebSocket($("#uri").val());
//連接成功建立后響應(yīng)
ws.onopen = function() {
log("成功連接到" + $("#uri").val());
}
//收到服務(wù)器消息后響應(yīng)
ws.onmessage = function(e) {
log("收到服務(wù)器消息:" + e.data + "'
");
}
//連接關(guān)閉后響應(yīng)
ws.onclose = function() {
log("關(guān)閉連接");
$("#disconnect").attr({"disabled":"disabled"});
$("#uri").removeAttr("disabled");
$("#connect").removeAttr("disabled");
ws = null;
}
$("#uri").attr({"disabled":"disabled"});
$("#connect").attr({"disabled":"disabled"});
$("#disconnect").removeAttr("disabled");
return false;
});
});
//發(fā)送字符串消息
$(function() {
$("#sendForm").submit(function() {
if (ws) {
var textField = $("#textField");
ws.send(textField.val());
log("我說:" + textField.val());
textField.val("");
textField.focus();
}
return false;
});
});
//發(fā)送arraybuffer(二進制文件)
$(function() {
$("#sendFileForm").submit(function() {
var inputElement = document.getElementById("file");
var fileList = inputElement.files;
for ( var i = 0; i <fileList.length; i++) {
console.log(fileList[i]);
log(fileList[i].name);
//發(fā)送文件名
ws.send(fileList[i].name);
// reader.readAsBinaryString(fileList[i]);
//讀取文件
var reader = new FileReader();
reader.readAsArrayBuffer(fileList[i]);
// reader.readAsText(fileList[i]);
//文件讀取完畢后該函數(shù)響應(yīng)
reader.onload = function loaded(evt) {
var binaryString = evt.target.result;
// Handle UTF-16 file dump
log("
開始發(fā)送文件");
ws.send(binaryString);
}
}
return false;
});
});
$(function() {
$("#disconnect").click(function() {
if (ws) {
$("#log").empty();
ws.close();
ws = null;
}
return false;
});
});
$(function() {
$("#reset").click(function() {
$("#log").empty();
return false;
});
});
</script>
</head>
<body>
<formid="uriForm">
<inputtype="text" id="uri" value="ws://192.168.31.37:7181"
style=" 200px;"> <inputtype="submit" id="connect"
value="Connect"><inputtype="button" id="disconnect"
value="Disconnect" disabled="disabled">
</form>
<br>
<formid="sendFileForm">
<inputid="file" type="file" multiple />
<inputtype="submit" value="Send" />
<inputtype="button" id="reset" value="清空消息框"/>
</form>
<br>
<formid="sendForm">
<inputtype="text" id="textField" value="">
<inputtype="submit" value="Send">
</form>
<br>
<form>
<textareaid="log" rows="30" cols="100"
style="font-family: monospace; color: red;"></textarea>
</form>
<br>
</body>
</html>
總結(jié)
以上是生活随笔 為你收集整理的C# socket编程 使用fleck轻松实现对话 https://github.com/statianzo/Fleck 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。