在js中调用WSS提供的要领是一个会经常运用到的东西,为一个js调用WSS的通用代码,直接拿过来用就可以。
核心部分跟Ajax的核心代码基本相同,都是先构造一个XMLHttpRequest或者ActiveXObject对象,然后就是XML的发送和接收。
SPAPI_Corefunction SPAPI_Core()
{
this.createXMLHttpRequest = function()
{
if (typeof XMLHttpRequest != "undefined")
{
return new XMLHttpRequest()
}
else if (typeof ActiveXObject != "undefined")
{
return new ActiveXObject("Microsoft.XMLHTTP")
}
else
{
throw new Error("XMLHttpRequest not supported")
}
}
this.executeRequest = function(serviceUrl, action, packet, params)
{
var oXMLHttpRequest = this.createXMLHttpRequest()
var result = null
var resultName
oXMLHttpRequest.open("POST", serviceUrl, false)
oXMLHttpRequest.setRequestHeader("Content-Type", "text/xml charset=utf-8")
oXMLHttpRequest.setRequestHeader("SOAPAction", action)
if (params != null)
{
for (var i=0 i < params.length i++)
{
packet = packet.replace('{' + i.toString() + '}', (params[i] == null ? '' : params[i]))
}
}
oXMLHttpRequest.send(packet)
resultName = action.substring(action.lastIndexOf('/') + 1) + 'Result'
'Result'
var resBatch
var status
var statusText
status = oXMLHttpRequest.status
statusText = oXMLHttpRequest.statusText
if (status == 200)
{
// Check for SharePoint error code
resBatch = oXMLHttpRequest.responseXML.getElementsByTagName(resultName)
var codeEl = oXMLHttpRequest.responseXML.getElementsByTagName('ErrorCode')
if (codeEl != null && codeEl.length > 0)
{
var spStatus = parseInt(codeEl[0].childNodes[0].nodeValue)
if (spStatus != 0)
{
status = 0-spStatus // Note we make this -ve to prevent confusion with the HTTP code
var messageEl = oXMLHttpRequest.responseXML.getElementsByTagName('ErrorText')
if (messageEl != null && messageEl.length >= 0)
{
statusText = messageEl[0].childNodes[0].nodeValue
}
}
}
}
result = {
status : status,
statusText : statusText,
responseXML : oXMLHttpRequest.responseXML,
responseText : oXMLHttpRequest.responseText,
resultNode : (resBatch == null resBatch.length == 0 ? null : resBatch[0])
}
return result
}
}
然后就是写WSS中各个要领的调用接口,WSS中提供的要领特别多,这里就不一一列举了,有需要可以追问。
<html>
<head>
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:7272")
//申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
ws.onopen = function(){
//当WebSocket创建成功时,触发onopen事件
console.log("open")
ws.send("hello")//将消息发送到服务端
}
ws.onmessage = function(e){
//当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
var data = JSON.parse(e.data)
switch(data['type']){
// 服务端ping客户端
case 'ping':
ws.send('{"type":"pong"}')
console.log("ping: "+e.data)
break
// 登录 更新用户列表
case 'login':
console.log("login: "+e.data)
//{"type":"login","client_id":xxx,"client_name":"xxx","client_list":"[...]","time":"xxx"}
/*say(data['client_id'], data['client_name'], data['client_name']+' 加入了聊天室', data['time'])
if(data['client_list'])
{
client_list = data['client_list']
}
else
{
client_list[data['client_id']] = data['client_name']
}
flush_client_list()
console.log(data['client_name']+"登录成功")*/
break
// 发言
case 'say':
console.log("say: "+e.data)
//{"type":"say","from_client_id":xxx,"to_client_id":"all/client_id","content":"xxx","time":"xxx"}
//say(data['from_client_id'], data['from_client_name'], data['content'], data['time'])
break
// 用户退出 更新用户列表
case 'logout':
console.log("logout: "+e.data)
//{"type":"logout","client_id":xxx,"time":"xxx"}
// say(data['from_client_id'], data['from_client_name'], data['from_client_name']+' 退出了', data['time'])
//delete client_list[data['from_client_id']]
// flush_client_list()
}
}
ws.onclose = function(e){
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件
console.log("close")
}
ws.onerror = function(e){
//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
console.log(error)
}
function login()
{
console.log("login:111")
var login_data = '{"type":"login","client_name":"zyx","room_id":"1"}'
console.log("websocket握手成功,发送登录数据:"+login_data)
ws.send(login_data)
}
function send()
{
console.log("send:111")
ws.send('{"type":"say","to_client_id":"all","to_client_name":"222","content":"txt"}')
}
function send2()
{
console.log("send:111")
ws.send('{"type":"say","to_client_id":"7f00000108fc00000011","to_client_name":"222","content":"txt"}')
}
</script>
</head>
<body>
<div onclick="login()" style="width:100pxheight:80px">
登录
</div>
<div onclick="send()" style="width:100pxheight:80px">
发送
</div>
<div onclick="send2()" style="width:100pxheight:80px">
发送2
</div>
</body>
</html>