Javascript客户端和C#服务器 [英] Javascript Client and C# server

查看:86
本文介绍了Javascript客户端和C#服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我一个简单的示例,说明如何连接这些2:

-在本地端口33333上工作的TCP侦听器服务器

-在浏览器上运行的Javascript客户端

我搜索了JS套接字,所有链接都转到了WebSockets或socket.io.如果服务器使用相同的框架创建,它们将非常有用.

你能帮忙吗? :D


客户端:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>

<script type="text/javascript">
// Create SocketIO instance, connect
var socket = new io.Socket(''localhost'',{
	port: 33333
});
socket.connect(); 

// Add a connect listener
socket.on(''connect'',function() {
	console.log(''Client has connected to the server!'');
});
// Add a connect listener
socket.on(''message'',function(data) {
	console.log(''Received a message from the server!'',data);
});
// Add a disconnect listener
socket.on(''disconnect'',function() {
	console.log(''The client has disconnected!'');
});

// Sends a message to the server via sockets
function sendMessageToServer(message) {
	socket.send(message);
}

</script>

<body> 

</body> 
</html>



JS中的此客户端代码甚至都没有连接到服务器.

提及:服务器与C#客户端一起使用.

我认为您只是看不到连接,因为在您在套接字上设置侦听器之前先执行connect调用.那么,如果在设置了侦听器之后移动了connect呼叫,那会发生什么呢?

或者,您可以尝试监视服务器程序以观察那里的客户端活动的任何痕迹.

问候,

曼弗雷德(Manfred)


所有您需要知道的是: http://code.google .com/p/bauglir-websocket/ [^ ]


Please give me an easy example on how can I connect these 2:

- TCP listener server working on localhost port 33333
and
- a Javascript client working on browser

I''ve searched JS sockets and all the links go to WebSockets or socket.io. They are very helpful if the server is created with the same framework.

Can you please help? :D

Edit:
Client side:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>

<script type="text/javascript">
// Create SocketIO instance, connect
var socket = new io.Socket(''localhost'',{
	port: 33333
});
socket.connect(); 

// Add a connect listener
socket.on(''connect'',function() {
	console.log(''Client has connected to the server!'');
});
// Add a connect listener
socket.on(''message'',function(data) {
	console.log(''Received a message from the server!'',data);
});
// Add a disconnect listener
socket.on(''disconnect'',function() {
	console.log(''The client has disconnected!'');
});

// Sends a message to the server via sockets
function sendMessageToServer(message) {
	socket.send(message);
}

</script>

<body> 

</body> 
</html>



This client code in JS doesn''t even connect to the server.

Mention: the server works with a C# client.

解决方案

Google for "TCP server C#" and you will find some easy samples that you can implement.


I think you are just not seeing the connect because you do the connect call before you have setup the listeners on your socket. So if you move the connect call after the listeners have been setup what happens then?

Alternately you could try to monitor the server program to observe any traces of client activity there.

Regards,

Manfred


All you need to know is here: http://code.google.com/p/bauglir-websocket/[^]


这篇关于Javascript客户端和C#服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆