本文整理汇总了C++中WebSocket类的典型用法代码示例。如果您正苦于以下问题:C++ WebSocket类的具体用法?C++ WebSocket怎么用?C++ WebSocket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WebSocket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: jsWebSocketPrototypeFunctionDispatchEvent
EncodedJSValue JSC_HOST_CALL jsWebSocketPrototypeFunctionDispatchEvent(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
if (!thisValue.inherits(&JSWebSocket::s_info))
return throwVMTypeError(exec);
JSWebSocket* castedThis = static_cast<JSWebSocket*>(asObject(thisValue));
WebSocket* imp = static_cast<WebSocket*>(castedThis->impl());
ExceptionCode ec = 0;
Event* evt(toEvent(exec->argument(0)));
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSC::JSValue result = jsBoolean(imp->dispatchEvent(evt, ec));
setDOMException(exec, ec);
return JSValue::encode(result);
}
开发者ID:13W,项目名称:phantomjs,代码行数:17,代码来源:JSWebSocket.cpp
示例2: run
void run(void* data) {
uint8_t buffer[1000];
WebSocket *pWebSocket = (WebSocket*) data;
// do something
Socket peerSocket = pWebSocket->getSocket();
ESP_LOGD(LOG_TAG, "Waiting on socket data for socket %s", peerSocket.toString().c_str());
int length = peerSocket.receive_cpp(buffer, sizeof(buffer));
ESP_LOGD(LOG_TAG, "Received data from web socket. Length: %d", length);
GeneralUtils::hexDump(buffer, length);
dumpFrame(*(Frame *)buffer);
// The following section parses the WebSocket frame.
if (length > 0) {
Frame* pFrame = (Frame*)buffer;
uint32_t payloadLen = 0;
uint8_t* pMask = nullptr;
uint8_t* pData;
if (pFrame->len < 126) {
payloadLen = pFrame->len;
pMask = buffer + 2;
} else if (pFrame->len == 126) {
payloadLen = *(uint16_t*)(buffer+2);
pMask = buffer + 4;
} else if (pFrame->len == 127) {
ESP_LOGE(LOG_TAG, "Too much data!");
return;
}
if (pFrame->mask == 1) {
pData = pMask + 4;
for (int i=0; i<payloadLen; i++) {
*pData = *pData ^ pMask[i%4];
pData++;
}
pData = pMask + 4;
} else {
pData = pMask;
}
std::string retData = std::string((char *)pData, payloadLen);
ESP_LOGD(LOG_TAG, "Resulting payload:");
GeneralUtils::hexDump(pData, payloadLen);
}
} // run
开发者ID:EdWeller,项目名称:esp32-snippets,代码行数:44,代码来源:WebSocket.cpp
示例3: disconnect
void SIOClientImpl::disconnect()
{
if(_ws->getReadyState() == WebSocket::State::OPEN)
{
std::string s = "0::";
_ws->send(s);
log("Disconnect sent");
_ws->close();
}
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
_connected = false;
SocketIO::getInstance()->removeSocket(_uri);
}
开发者ID:Abioy,项目名称:OpenBird,代码行数:19,代码来源:SocketIO.cpp
示例4: jsWebSocketPrototypeFunctionSend
EncodedJSValue JSC_HOST_CALL jsWebSocketPrototypeFunctionSend(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
if (!thisValue.inherits(&JSWebSocket::s_info))
return throwVMTypeError(exec);
JSWebSocket* castedThis = static_cast<JSWebSocket*>(asObject(thisValue));
WebSocket* imp = static_cast<WebSocket*>(castedThis->impl());
if (exec->argumentCount() < 1)
return JSValue::encode(jsUndefined());
ExceptionCode ec = 0;
const String& data(ustringToString(exec->argument(0).toString(exec)));
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSC::JSValue result = jsBoolean(imp->send(data, ec));
setDOMException(exec, ec);
return JSValue::encode(result);
}
开发者ID:13W,项目名称:phantomjs,代码行数:19,代码来源:JSWebSocket.cpp
示例5: WebSocket
void WebSocketServer::listen() {
// First check existing connections:
for( byte x=0; x < m_maxConnections; x++ )
{
if( !m_connections[x] )
continue;
WebSocket *s = m_connections[x];
if( !s->isConnected() )
{
m_connectionCount--;
delete s;
m_connections[x] = NULL;
continue;
}
s->listen();
}
EthernetClient cli = m_server.available();
if( !cli )
return;
// Find a slot:
for( byte x=0; x < m_maxConnections; x++ )
{
if( m_connections[x] )
continue;
WebSocket *s = new WebSocket(this, cli);
m_connections[x] = s;
m_connectionCount++;
#ifdef DEBUG
Serial.println(F("Websocket client connected."));
#endif
return;
}
// No room!
#ifdef DEBUG
Serial.println(F("Cannot accept new websocket client, maxConnections reached!"));
#endif
cli.stop();
}
开发者ID:KillerGoldFisch,项目名称:ArduinoWebsocketServer,代码行数:43,代码来源:WebSocket.cpp
示例6: checkWebSocket
/**
* @author John M. Harris, Jr.
* @internal
*/
int WebSocket::lua_close(lua_State* L){
WebSocket* LuaWebSocket = checkWebSocket(L, 1);
if(LuaWebSocket){
if(LuaWebSocket->m_webSocket){
if(LuaWebSocket->m_webSocket->getState() == QAbstractSocket::ConnectedState){
ob_enum::LuaEnumItem* val = ob_enum::checkEnumItem(L, 2, ob_enum::LuaCloseCode);
ob_enum::CloseCode cc = ob_enum::CloseCode::Normal;
if(val){
cc = (ob_enum::CloseCode)val->value;
}
QString reason;
if(lua_isstring(L, 3)){
reason = QString(lua_tostring(L, 3));
}
LuaWebSocket->close(cc, reason);
return 0;
}
}
luaL_error(L, "WebSocket is already closed.");
}
return 0;
}
开发者ID:RobloxLabs,项目名称:OpenBlox,代码行数:26,代码来源:WebSocket.cpp
示例7: WebSocket
bool HttpServer::initWebSocket(HttpServerConnection& connection, HttpRequest& request, HttpResponse& response)
{
if (!wsEnabled)
return false;
WebSocket *sock = new WebSocket(&connection);
if (!sock->initialize(request, response))
return false;
connection.setTimeOut(USHRT_MAX); //Disable disconnection on connection idle (no rx/tx)
connection.setDisconnectionHandler(HttpServerConnectionDelegate(&HttpServer::onCloseWebSocket, this)); // auto remove on close
response.sendHeader(connection); // Will push header before user data
wsocks.addElement(sock);
if (wsConnect) wsConnect(*sock);
if (wsCommandEnabled && (request.getQueryParameter(wsCommandRequestParam) == "true"))
{
debugf("WebSocket Commandprocessor started");
sock->enableCommand();
}
}
开发者ID:fernandomorse,项目名称:Sming,代码行数:22,代码来源:HttpServer.cpp
示例8: _webSocketData
int Server::_webSocketData(struct mg_connection *conn, string data)
{
WebSocket *websocket = websockets.getWebSocket(conn);
if (websocket != NULL) {
websocket->appendData(data);
string fullPacket = websocket->flushData();
vector<Controller *>::iterator it;
for (it=controllers.begin(); it!=controllers.end(); it++) {
(*it)->webSocketData(websocket, fullPacket);
}
if (websocket->isClosed()) {
websockets.remove(websocket);
return 0;
} else {
return -1;
}
} else {
return 0;
}
}
开发者ID:GangWang,项目名称:mongoose-cpp,代码行数:23,代码来源:Server.cpp
示例9: log
void SIOClientImpl::openSocket()
{
log("SIOClientImpl::openSocket() called");
std::stringstream s;
s << _uri << "/socket.io/1/websocket/" << _sid;
_ws = new WebSocket();
if (!_ws->init(*this, s.str()))
{
CC_SAFE_DELETE(_ws);
}
return;
}
开发者ID:Abioy,项目名称:OpenBird,代码行数:15,代码来源:SocketIO.cpp
示例10: WebSocket
/*!
\internal
*/
WebSocket *WebSocket::upgradeFrom(QTcpSocket *pTcpSocket,
const HandshakeRequest &request,
const HandshakeResponse &response,
QObject *parent)
{
WebSocket *pWebSocket = new WebSocket(pTcpSocket, response.getAcceptedVersion(), parent);
pWebSocket->setExtension(response.getAcceptedExtension());
pWebSocket->setOrigin(request.getOrigin());
pWebSocket->setRequestUrl(request.getRequestUrl());
pWebSocket->setProtocol(response.getAcceptedProtocol());
pWebSocket->setResourceName(request.getRequestUrl().toString(QUrl::RemoveUserInfo));
pWebSocket->enableMasking(false); //a server should not send masked frames
return pWebSocket;
}
开发者ID:Jasonic,项目名称:IanniX,代码行数:18,代码来源:websocket.cpp
示例11: QCOMPARE
void WebSocketTest::properties()
{
WebSocket websocket;
QCOMPARE(websocket.messagesType(), WebSocket::BINARY_MESSAGE);
websocket.setMessagesType(WebSocket::TEXT_MESSAGE);
QCOMPARE(websocket.messagesType(), WebSocket::TEXT_MESSAGE);
websocket.setMessagesType(WebSocket::BINARY_MESSAGE);
QCOMPARE(websocket.messagesType(), WebSocket::BINARY_MESSAGE);
}
开发者ID:knopkem,项目名称:nordwind,代码行数:12,代码来源:websocket.cpp
示例12: main
int main( int argc, const char** argv )
{
int camera_number = 0;
char socket_url[512];
TopCodeScanner scanner;
WebSocket *socket = NULL;
if (argc < 2) {
cerr << "expected: " << argv[0] << " <camera_number> [socket server]" << endl;
cerr << " example: > topcodes 0 ws://localhost:8126/topcodes" << endl;
return -1;
}
// get the camera number
camera_number = atoi(argv[1]); // 0 if error
if (argc >= 3) {
socket = WebSocket::from_url(argv[2]);
} else {
socket = WebSocket::from_url("ws://localhost:8126/topcodes");
}
// open the default camera
VideoCapture cap(camera_number);
if (!cap.isOpened()) {
cerr << "Error: Unable to open webcam " << camera_number << endl;
return -1;
}
for(;;)
{
Mat frame, grey, flipped;
// capture the next still video frame
cap >> frame;
// flip the image horizontally so that it gives you a mirror reflection
flip(frame, flipped, 1);
// convert to greyscale
cvtColor(flipped, grey, CV_RGB2GRAY);
// scan for topcodes
vector<TopCode*> *codes = scanner.scan(grey);
// send topcode info through the websocket
if (socket) {
string json = "[\n";
for (int i=0; i<codes->size(); i++) {
TopCode *code = (*codes)[i];
json += (" " + code->toJSON() + ",\n");
}
json += "]";
socket->send(json);
socket->poll();
socket->dispatch(handle_message);
}
// show the resulting image (debuggin)
imshow("webcam", grey);
// press the 'q' key to quit
if (waitKey(30) >= 0) break;
}
if (socket) delete socket;
}
开发者ID:TIDAL-Lab,项目名称:TopCodes,代码行数:68,代码来源:WebCam.cpp
示例13: sendTextFrame
void sendTextFrame(WebSocket& ws, const std::string& s)
{
ws.sendFrame(s.data(), s.size());
}
开发者ID:wanweihua,项目名称:online,代码行数:4,代码来源:LoadTest.cpp
示例14: js_cocos2dx_extension_WebSocket_constructor
bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (argc == 1 || argc == 2)
{
std::string url;
do {
bool ok = jsval_to_std_string(cx, args.get(0), &url);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
JS::RootedObject proto(cx, js_cocos2dx_websocket_prototype);
JS::RootedObject obj(cx, JS_NewObject(cx, js_cocos2dx_websocket_class, proto, JS::NullPtr()));
//JS::RootedObject obj(cx, JS_NewObjectForConstructor(cx, js_cocos2dx_websocket_class, args));
WebSocket* cobj = new (std::nothrow) WebSocket();
JSB_WebSocketDelegate* delegate = new (std::nothrow) JSB_WebSocketDelegate();
delegate->setJSDelegate(obj);
if (argc == 2)
{
std::vector<std::string> protocols;
if (args.get(1).isString())
{
std::string protocol;
do {
bool ok = jsval_to_std_string(cx, args.get(1), &protocol);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
protocols.push_back(protocol);
}
else if (args.get(1).isObject())
{
bool ok = true;
JS::RootedObject arg2(cx, args.get(1).toObjectOrNull());
JSB_PRECONDITION(JS_IsArrayObject( cx, arg2 ), "Object must be an array");
uint32_t len = 0;
JS_GetArrayLength(cx, arg2, &len);
for( uint32_t i=0; i< len;i++ )
{
JS::RootedValue valarg(cx);
JS_GetElement(cx, arg2, i, &valarg);
std::string protocol;
do {
ok = jsval_to_std_string(cx, valarg, &protocol);
JSB_PRECONDITION2( ok, cx, false, "Error processing arguments");
} while (0);
protocols.push_back(protocol);
}
}
cobj->init(*delegate, url, &protocols);
}
else
{
cobj->init(*delegate, url);
}
JS_DefineProperty(cx, obj, "URL", args.get(0), JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
//protocol not support yet (always return "")
JS::RootedValue jsprotocol(cx, c_string_to_jsval(cx, ""));
JS_DefineProperty(cx, obj, "protocol", jsprotocol, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
// link the native object with the javascript object
js_proxy_t *p = jsb_new_proxy(cobj, obj);
JS::AddNamedObjectRoot(cx, &p->obj, "WebSocket");
args.rval().set(OBJECT_TO_JSVAL(obj));
return true;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
return false;
}
开发者ID:ktjooho,项目名称:machine-learning,代码行数:82,代码来源:jsb_websocket.cpp
示例15: wsMessageReceived
void wsMessageReceived(WebSocket& socket, const String& message)
{
Serial.printf("WebSocket message received:\r\n%s\r\n", message.c_str());
String response = "Echo: " + message;
socket.sendString(response);
}
开发者ID:BorntraegerMarc,项目名称:Sming,代码行数:6,代码来源:application.cpp
示例16: WebSocket
void DirectWebReader::read()
{
WebSocket *socket = new WebSocket(m_request, this, new WebSocketOutput(m_socketHandler));
socket->readRequest();
}
开发者ID:OwNet,项目名称:qtownet,代码行数:5,代码来源:directwebreader.cpp
示例17: wsMessageReceived
void wsMessageReceived(WebSocket& socket, const String& message) {
//Serial.printf("WebSocket message received:\r\n%s\r\n", message.c_str());
char buf[22];
dtostrf(pf, 10, 8, buf);
socket.sendString(buf);
}
开发者ID:zhivko,项目名称:Sming,代码行数:6,代码来源:application.cpp
示例18: js_cocos2dx_extension_WebSocket_constructor
JSBool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, jsval *vp)
{
jsval *argv = JS_ARGV(cx, vp);
if (argc == 1 || argc == 2)
{
std::string url;
do {
JSBool ok = jsval_to_std_string(cx, argv[0], &url);
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments");
} while (0);
JSObject *obj = JS_NewObject(cx, js_cocos2dx_websocket_class, js_cocos2dx_websocket_prototype, NULL);
WebSocket* cobj = new WebSocket();
JSB_WebSocketDelegate* delegate = new JSB_WebSocketDelegate();
delegate->setJSDelegate(obj);
if (argc == 2)
{
std::vector<std::string> protocols;
if (JSVAL_IS_STRING(argv[1]))
{
std::string protocol;
do {
JSBool ok = jsval_to_std_string(cx, argv[1], &protocol);
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments");
} while (0);
protocols.push_back(protocol);
}
else if (argv[1].isObject())
{
JSBool ok = JS_TRUE;
JSObject* arg2 = JSVAL_TO_OBJECT(argv[1]);
JSB_PRECONDITION(JS_IsArrayObject( cx, arg2 ), "Object must be an array");
uint32_t len = 0;
JS_GetArrayLength(cx, arg2, &len);
for( uint32_t i=0; i< len;i++ )
{
jsval valarg;
JS_GetElement(cx, arg2, i, &valarg);
std::string protocol;
do {
ok = jsval_to_std_string(cx, valarg, &protocol);
JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments");
} while (0);
protocols.push_back(protocol);
}
}
cobj->init(*delegate, url, &protocols);
}
else
{
cobj->init(*delegate, url);
}
JS_DefineProperty(cx, obj, "URL", argv[0]
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
//protocol not support yet (always return "")
JS_DefineProperty(cx, obj, "protocol", c_string_to_jsval(cx, "")
, NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY);
// link the native object with the javascript object
js_proxy_t *p = jsb_new_proxy(cobj, obj);
JS_AddNamedObjectRoot(cx, &p->obj, "WebSocket");
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
return JS_TRUE;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0);
return JS_FALSE;
}
开发者ID:6520874,项目名称:pipiGame,代码行数:82,代码来源:jsb_websocket.cpp
示例19: main
//.........这里部分代码省略.........
session.sendRequest(req);
StringTokenizer tokenizer(session.socket().address().toString(), ":", StringTokenizer::TOK_TRIM); // get the request originating address:ip from the session socket initiated by HTPP; tokenize it for IP extraction
//string my_mac = getMAC((tokenizer[0]).c_str()); // call IP to MAC converter
// get response
HTTPResponse res;
istream& is = session.receiveResponse(res); // stream the request
cout << res.getStatus() << " " << res.getReason() << endl; // get the status code of the transaction
// convert the istream to sting for further processing
istreambuf_iterator<char> eos;
string s(istreambuf_iterator<char>(is), eos);
const char * cc = s.c_str();
// instantiate a rapidjson document and fill it up with the response of the negotiation request
rapidjson::Document document;
document.Parse<0>(cc);
string token = document["ConnectionToken"].GetString(); // parse the response and get the connectionToken
//=============================================
//connect to signarR using the connectionToken got previously
HTTPClientSession cs(signalr_service_url, signalr_service_port); // instantiate simple webclient
string what = "/" + signalr_url_endpoint + "/connect?transport=webSockets&connectionToken=" + urlencode(token) + "&UID=" + my_mac + "&connectionData=%5B%7B%22name%22%3A%22myhub%22%7D%5D&tid=10"; // compose the request string
HTTPRequest request(HTTPRequest::HTTP_GET, what, "HTTP/1.1"); // the protocol MUST be HTTP/1.1, as described in RFC6455; else the UPGRADE to websocket request will fail
request.set("Host", signalr_service_url); // specify the Host header to be sent
HTTPResponse response; // instantiate a http response
cout << response.getStatus() << " " << response.getReason() << endl;
WebSocket * ws = new WebSocket(cs, request, response); // instantiate a WebSocket transaction to the 'cs' session, sending the 'request' and storing the 'response'
//sample of a message to be sent
payload = "{\"H\":\"myhub\",\"M\":\"Send\",\"A\":[\"" + my_mac + "\",\"invoked from " + replaceInPlace(my_mac, std::string(":"), std::string("-")) + " client\"],\"I\":0}";
// cout << endl << payload << endl ;
ws->sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT); // send the message to signalR using the payload and setting the type of frame to be sent as FRAME_TEXT
flags = 1;
// starting the receiving loop
while( (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE ) // while websocket session open
{
n = ws->receiveFrame(buffer, sizeof(buffer), flags); // n is the received frame
// flags store the response flags of the frame
// 129 = single frame response
// 1 = start of multi-frame response
// 0 = continuation frame of a multi-frame response
// 128 = FIN frame os a multi-frame response
// signalR send data in JSON format, and it send empty messages (empty json document) on a regular basis
if( (n != 2) && flags !=1 ) // filter out empty jsons and multiframe responses (multiframe will be treated further on)
{
cout << "RCV[" << msg_cnt << "]=> " << buffer << " ===== " << unsigned(flags) << endl;
}
if(flags == 1){ // if I get a start frame of a multi-frame response means that I am getting something usefull from signalR
string str(buffer);
out += str;
// due to flag == 1, we are expecting several frames, until we got flag == 128
do{
n = ws->receiveFrame(buffer, sizeof(buffer), flags);
string str(buffer);
out += str; // we add the next frame/frames to the out variable, to construct the whole JSON message
开发者ID:VirtualLife,项目名称:piSocket,代码行数:67,代码来源:piSocket.cpp
示例20: wsThreadEntryFunc
void* WsThreadHelper::wsThreadEntryFunc(void* arg)
{
_ws->onSubThreadStarted();
while (!_needQuit)
{
if (_ws->onSubThreadLoop())
{
break;
}
}
_ws->onSubThreadEnded();
return (void*)0;
}
开发者ID:1085075003,项目名称:quick-cocos2d-x,代码行数:16,代码来源:WebSocket.cpp
注:本文中的WebSocket类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论